Mawk printf %d maxes out at 2147483647


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Mawk printf %d maxes out at 2147483647
# 8  
Old 01-17-2014
The old SysV Unix /bin/oawk and /bin/nawk have the same limit:
Code:
echo 2147483649 | /bin/oawk '{printf "%d\n", $1}'
2147483647
echo 2147483649 | /bin/nawk '{printf "%d\n", $1}'
2147483647

While print has a higher limit
Code:
echo 2147483649 | /bin/oawk '{print $1}'
2147483649
echo 2147483649 | /bin/nawk '{print $1}'
2147483649

# 9  
Old 01-17-2014
@MadeinGermany, the latter is because the numbers are interpreted as a string:
Code:
$ echo 214744445564646464646454646646464646466666646456456546456456466464646483649 | awk '{print $1}'
214744445564646464646454646646464646466666646456456546456456466464646483649

# 10  
Old 01-17-2014
Oh I overlooked that, have to force a number:
Code:
echo 2147483649 | /bin/oawk '{print $1+0}'
2147483649
echo 2147483649 | /bin/nawk '{print $1+0}'
2147483649

Interesting: GNU awk immediately escapes to floating point here.
# 11  
Old 01-17-2014
What I think the standard says (whether or not it matters, is another matter).

POSIX mandates doubles for all AWK numerics [1]:
Quote:
A string value shall be converted to a numeric value either by the equivalent of the following calls to functions defined by the ISO C standard:

setlocale(LC_NUMERIC, "");
numeric_value = atof(string_value);

or by converting the initial portion of the string to type double representation as follows: ...
So, at least at the outset, the valid integral range is approximately -2^53 to 2^53.

What should happen when you try to print the integer using printf "%d" bigint? POSIX AWK says [1]:
Quote:
The printf statement shall produce output based on a notation similar to the File Format Notation used to describe file formats in this volume of POSIX.1-2008 (see XBD File Format Notation).
The File Format Notation [2] section then says:
Quote:
d,i,o,u,x,X
The integer argument shall be written as signed decimal ( d or i )
Note the use of "integer", not "int" as is the case in the C library implementation. The latter is a specific data type, the former is not. In this context, what exactly is an "integer"?

In Concepts Derived from the ISO C Standard [3], POSIX says:
Quote:
Integer variables and constants, including the values of operands and option-arguments, used by the standard utilities listed in this volume of POSIX.1-2008 shall be implemented as equivalent to the ISO C standard signed long data type
So, it seems to me that POSIX requires compliant AWK implementations to store all values as doubles and to use at least signed long when converting an integral double to an integral type.

If this is correct, a compliant AWK implementation on an LP64 platform (most 64-bit UNIX) should never lose precision when converting a double to an integer.

I know that nawk does indeed store all numerics as doubles. However, it casts to signed int during printf "%d" ....

References:
[1] POSIX: AWK
[2] POSIX: File Format Notation
[3] POSIX: Concepts Derived from the ISO C Standard

Regards,
Alister

Last edited by alister; 01-17-2014 at 04:57 PM..
These 2 Users Gave Thanks to alister For This Post:
# 12  
Old 01-17-2014
Interesting, and neither mawk (typical versions) nor nawk are POSIX compliant. Case in point on Solaris:
Code:
$ echo 2147483649 | /bin/nawk '{printf "%d\n", $1}'
2147483647
$ echo 2147483649 | /usr/xpg4/bin/awk '{printf "%d\n", $1}'
2147483649


Last edited by Scrutinizer; 01-17-2014 at 05:20 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

[SOLVED] Making mktime/strftime available to mawk

I frequently use awk time functions and am switching some scripts over to mawk. I don't have the mktime or strftime functions in mawk, but it appears that there is a way, as explained here in "Time functions": Please only cut-and-past links to man pages from our man pages. So, simple... (10 Replies)
Discussion started by: treesloth
10 Replies

2. Shell Programming and Scripting

MAWK does not support length(array)?

As Brendan O'Conner writes in this blog, mawk is near 8 times faster than gawk, so I am going to give mawk a go, but I got errors when trying to print the length of an array in mawk using length() function, is it not supported in mawk? or there's another way to get the length of an array in mawk? ... (3 Replies)
Discussion started by: kevintse
3 Replies

3. UNIX for Dummies Questions & Answers

The meaning of %s in printf

I have this command like that has %s in it, I know %s calls a column, but I am not sure I understand which column (I mean for my case I can check the input file, but I want to know how is this %s used, how comes tha same symbo; gives different columns in one command line: {printf "grep %s... (22 Replies)
Discussion started by: cosmologist
22 Replies

4. UNIX for Dummies Questions & Answers

mawk script to compare 2 files and report where they match

I have two files and would like a report of where they match. Example of file1: 1 1 1 2 2 2 13 14 15 4 4 4 15 16 17 100 102 1004 56 57 890 Example of file2: 2 2 2 16 10 11 45 22 35 13 14 15 1001 1002 3456 100 102 1004 (1 Reply)
Discussion started by: kenneth.mcbride
1 Replies

5. UNIX for Dummies Questions & Answers

Need help with printf

Hi, I have just completed my first script (:D) and now i just need to format it with printf. This is what I have: #!/bin/ksh TOTB=0 TOTF=0 TOTI=0 HOST=`hostname` echo " FSYSTEM BLKS FREE INUSE MOUNTEDON" df -m | grep -v ":"|grep -v Free|grep -v "/proc"| while read FSYSTEM... (2 Replies)
Discussion started by: compan023
2 Replies

6. Shell Programming and Scripting

IF and awk/printf

Hi Friends, Scripting newb here. So I'm trying to create a geektool script that uses awk and printf to output certain fields from top (namely command, cpu%, rsize, pid and time, in that order). After much trial and error, I've pretty much succeeded, with one exception. Any process whose name... (3 Replies)
Discussion started by: thom.mattson
3 Replies

7. Shell Programming and Scripting

awk and printf

echo $bbsize 1.5 echo $fillpercent .95 echo $bbsize | awk '{printf "%.2f\n",$0*$fillpercent}' 2.25 echo $bbsize | awk '{printf "%.2f\n",$0*.95}' 1.42 1.42 is what I'm expecting... echo $blocksize 4096 echo $bbsize | awk '{printf "%.2f\n",$0*$blocksize}' 2.25 echo $bbsize |... (3 Replies)
Discussion started by: xgringo
3 Replies

8. Shell Programming and Scripting

printf in awk

Hi friends.. I am confused about awk printf option.. I have a comma separated file 88562848,21-JAN-08,2741079, -1188,-7433,TESTING 88558314,21-JAN-08,2741189, -1273,-7976,TESTING and there is a line in my script ( written by someone else) What is the use of command? I guess... (10 Replies)
Discussion started by: clx
10 Replies

9. Shell Programming and Scripting

printf

How to print output in following format? A..................ok AA................ok AAA..............ok AAAAAA........ok "ok" one under one (4 Replies)
Discussion started by: mirusnet
4 Replies

10. Programming

printf

What is the output of the following program considering an x86 based parameter passing sequence where stack grows towards lower memory addresses and that arguments are evaluated from right to left: int i=10; int f1() { static int i = 15; printf("f1:%d ", i); return i--; } main() {... (2 Replies)
Discussion started by: arunviswanath
2 Replies
Login or Register to Ask a Question