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:
10 More Discussions You Might Find Interesting
1. Programming
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
2. Shell Programming and Scripting
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
3. Shell Programming and Scripting
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
4. Shell Programming and Scripting
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
5. Shell Programming and Scripting
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
6. UNIX for Dummies Questions & Answers
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
7. UNIX for Dummies Questions & Answers
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
8. UNIX for Dummies Questions & Answers
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
9. Shell Programming and Scripting
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
10. UNIX for Advanced & Expert Users
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