bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: gawk number to string bug


From: Andrew J. Schorr
Subject: Re: gawk number to string bug
Date: Wed, 21 Dec 2005 18:04:53 -0500
User-agent: Mutt/1.4.1i

On Wed, Dec 21, 2005 at 04:58:00PM -0500, Andrew J. Schorr wrote:
> So I do see one difference in behavior between your patch and mine,
> and that's on a Solaris 8 32-bit sparc platform.  Using the test
> case:
> 
>     gawk --lint 'BEGIN { x=2**60; for(i=60;i<=65;i++) { printf "2^%d= %s %d 
> %g\n",i,x,x,x; x*=2}}'
> 

And I have one other question: in builtin.c:format_tree(), why not simply
use snprintf("%.0f") to produce "%d"?  That would eliminate the whole problem
in format_tree, wouldn't it?  Is there some reason we want to do the
formatting ourselves instead of using snprintf?

On 32-bit x86 linux (with Paul's patch):

$ ./gawk --lint 'BEGIN { x=2^60; for(i=60;i<=65;i++) { y = sprintf("%d",x); z = 
sprintf("%.0f",x); print x,y,z,x-y,x-z; x*=2}}'               
1.15292e+18 1152921504606846976 1152921504606846976 0 0
2.30584e+18 2305843009213693952 2305843009213693952 0 0
4.61169e+18 4611686018427387904 4611686018427387904 0 0
9.22337e+18 9223372036854775808 9223372036854775808 0 0
gawk: warning: [s]printf: value 1.84467e+19 is out of range for `%d' format
1.84467e+19 1.84467e+19 18446744073709551616 4.40737e+13 0
gawk: warning: [s]printf: value 3.68935e+19 is out of range for `%d' format
3.68935e+19 3.68935e+19 36893488147419103232 -1.18526e+13 0

And on 64-bit x86 opteron:
$ ./gawk --lint 'BEGIN { x=2^60; for(i=60;i<=65;i++) { y = sprintf("%d",x); z = 
sprintf("%.0f",x); print x,y,z,x-y,x-z; x*=2}}'               
1152921504606846976 1152921504606846976 1152921504606846976 0 0
2305843009213693952 2305843009213693952 2305843009213693952 0 0
4611686018427387904 4611686018427387904 4611686018427387904 0 0
9.22337e+18 9223372036854775808 9223372036854775808 0 0
gawk: warning: [s]printf: value 1.84467e+19 is out of range for `%d' format
1.84467e+19 1.84467e+19 18446744073709551616 44073709551616 0
gawk: warning: [s]printf: value 3.68935e+19 is out of range for `%d' format
3.68935e+19 3.68935e+19 36893488147419103232 -11852580896768 0

So it seems to me that if we implemented %d internally using snprintf("%.0f"),
then the results would be pretty nice.

Regards,
Andy




reply via email to

[Prev in Thread] Current Thread [Next in Thread]