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

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

Re: GAWK adding big numbers wrong


From: laura fairhead
Subject: Re: GAWK adding big numbers wrong
Date: Mon, 13 May 2002 20:22:45 GMT

On Sat, 11 May 2002 18:34:07 +0200, "Bernd Schueller" <address@hidden> wrote:

>Hello,
>
>When calculating _big_ numbers with 'gawk' I get wrong results with
>numbers larger than 53 bits (dec. > 9007199254740991)!
>
>The program:
>---
>gawk -v sum=1 '
>BEGIN {
> for (i=1 ; i<57; i++)
> {printf("bit: %d\tvalue: %.f\tsum of %d bits:\t%.f\n" , i, sum, i,
>summe+=sum) ; sum=sum*2}
> }
>'
>---
>
>generates:
>..
>bit: 52 value: 2251799813685248 sum of 52 bits: 4503599627370495
>bit: 53 value: 4503599627370496 sum of 53 bits: 9007199254740991
>bit: 54 value: 9007199254740992 sum of 54 bits: 18014398509481984 *WRONG*
>bit: 55 value: 18014398509481984        sum of 55 bits: 36028797018963968
>..
>
>The program:
>---
>gawk ' BEGIN { for (i=9007199254740988 ; i<9007199254740994 ; i++) printf
>"%.f\n",i } '
>---
>
>results in an infinite loop, continiously printing: 9007199254740992.
>
>
>I am using 'GNU Awk 3.0.4' from the 'Cygwin Package' on a Windows system:
>
>Package             Version
>bash                2.05a-3
>gawk                3.0.4-1
>
>System is:
>Microsoft Windows 98 4.10.2222 A
>GenuineIntel x86 Family 6 Model 8 Stepping 6 (Celeron 700MHz)
>FPU built-in
>
>
>I do not suggest a CPU/FPU or Win98 problem, as I can add up the numbers
>correctly using the calculator tool from the Win98 menue...?!
>

The calculator tool is probably using some sort of special library
routines to add the big numbers up with. 'awk' uses the floating point
unit in the processor, floating point works by having a certain number
of bits for a mantissa (with x86 I think it's usually 53)  and a certain
number for an exponent. The actual value x will be represented as
approximately 'mantissa + 2^exponent', so beyond a certain value you
don't have contiguous integars any more and that is causing your problem.

If you overflow the mantissa bits a value can no longer be represented
exactly and you get an approximation, eg;

11111111111111111111111......111111     ....any.....
52_______Mantissa_________________0     __exponent__

+2 and the number of bits of the real value is going to be 54 which
can't be held in the mantissa any more. It should be '2^53+1' however
since that can't be represented it's more likely to be just 2^53 (or
maybe 2^53+2, I haven't read the f.p specs yet <g>). It's possible to
write a little loop that tests successive powers of 2 incremented by
one like this to find the number of bits you have available. If you need
to deal with large integar numbers in 'awk' like this either use a pipeline
to a utility like 'dc' (which handles arbitarily big values) or get
a 'bignum' function library for 'awk' (either write your own or search
the internet for some scripts :-)

>
>Thank you!
>
>Best regards
> bs
>--
>Bernhard Schueller
>Tilsiter Weg 4
>D-68723 Schwetzingen
>mailto:address@hidden
>

buenas noches

>

-- 
laura fairhead  # address@hidden  http://lf.8k.com
                # if you are bored crack my sig.
1F8B0808CABB793C0000666667002D8E410E83300C04EF91F2877D00CA138A7A
EAA98F30C494480157B623C4EF1B508FDED1CEFA9152A23DE35D661593C5318E
630C313CD701BE92E390563326EE17A3CA818F5266E4C2461547F1F5267659CA
8EE2092F76C329ED02CA430C5373CC62FF94BAC6210B36D9F9BC4AB53378D978
80F2978A1A6E5D6F5133B67B6113178DC1059526698AFE5C17A5187E7D930492



reply via email to

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