On 2016-09-20, pop <
p_...@hotmail.com> wrote:
> why does this work?:
> gawk "BEGIN{print 2^54,2^54-2}"
> 18014398509481984 18014398509481982
>
> but this does not work?:
> gawk "BEGIN{print 2^54,2^54-1}"
> 18014398509481984 18014398509481984
>
> all powers greater than 53 display this strange behavior. Is this a bug
Yes, because 53 is how many binary digits of precision there are in
an IEE 754 double floating point mantissa. (Actually 52 are stored;
there is an implied 1 which is not stored).
So that is the limit on the range of integers that can be represented
exactly.
> or is this the way it is supposed to work with 64bit integers? I noticed
It's not working with integers there, 64 bit or otherwise.
> the similar behavior with windows API calls with powers greater than 53.
You need to pass -M to gawk to get it to use multi-precision arithmetic:
Wrong output:
$ gawk "BEGIN{print 2^130,2^130-1}"
1361129467683753853853498429727072845824 1361129467683753853853498429727072845824
Right output:
0:rocktron:~/txr-64$ gawk -M "BEGIN{print 2^130,2^130-1}"
1361129467683753853853498429727072845824 1361129467683753853853498429727072845823
For that, we need Awk compiled with the right libs:: GNU MP and MPFR:
$ gawk --version
GNU Awk 4.1.4, API: 1.1 (GNU MPFR 3.1.4, GNU MP 6.1.1)
Copyright (C) 1989, 1991-2016 Free Software Foundation.
> GNU Awk 4.1.3
> Windows 10.0.10586 N/A Build 10586
> 4 GB memory
This might a Windows build made with MinGW; not sure if that is linked
with those libraries for -M support.
Try the experimental Cygnal-ized build I made (warning: 64 bit, not 32):
http://www.kylheku.com/cygnal/winawk.tar.gz
Test:
C:\Users\kaz\Desktop\winawk>dir
Volume in drive C is Local Disk
Volume Serial Number is 5E2B-F3C2
Directory of C:\Users\kaz\Desktop\winawk
09/20/2016 02:10 PM <DIR> .
09/20/2016 02:10 PM <DIR> ..
08/28/2016 05:05 PM 71,187 cyggcc_s-seh-1.dll
08/28/2016 05:05 PM 539,155 cyggmp-10.dll
08/28/2016 05:05 PM 1,033,235 cygiconv-2.dll
08/28/2016 05:05 PM 40,979 cygintl-8.dll
08/28/2016 05:05 PM 353,299 cygmpfr-4.dll
08/28/2016 05:05 PM 285,203 cygncursesw-10.dll
08/28/2016 05:05 PM 211,475 cygreadline7.dll
08/28/2016 05:08 PM 3,510,225 cygwin1.dll
08/28/2016 05:08 PM 571,923 gawk.exe
9 File(s) 6,616,681 bytes
2 Dir(s) 140,814,749,696 bytes free
C:\Users\kaz\Desktop\winawk>gawk --version
GNU Awk 4.1.4, API: 1.1 (GNU MPFR 3.1.4, GNU MP 6.1.1)
Copyright (C) 1989, 1991-2016 Free Software Foundation.
[ ... etc ... ]
C:\Users\kaz\Desktop\winawk>gawk -M "BEGIN{print 2^130,2^130-1}"
1361129467683753853853498429727072845824 136112946768375385385349842972707284582
3