[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] How does awk implement extremely long integers?
From: |
Davide Brini |
Subject: |
Re: [bug-gawk] How does awk implement extremely long integers? |
Date: |
Tue, 31 Jan 2012 19:10:24 +0100 |
On Tue, 31 Jan 2012 09:03:05 -0600, Peng Yu <address@hidden> wrote:
> Hi,
>
> I don't see where the limit of an integer in gawk is documented. But I
> tried the following code on my MacBook Pro (64bit) with macports
> installation. It can store integer as large as 2^1023. (I suspect the
> maximum should be something like 2^1024-1, but I never test it). I'm
> wondering how gawk store and compute some big integer, as an integer
> of this large is beyond any native type supported my the machine.
>
> If some library is used to support long integers, why not use one that
> supports infinitely large integer?
>
> ~$ seq 1023|awk 'BEGIN{i=1}{i*=2}END{print i}'
> 89884656743115795386465259539451236680898848947115328636715040578866337902750481566354238661203768010560056939935696678829394884407208311246423715319737062188883946712432742638151109800623047059726541476042502884419075341171231440736956555270413618581675255342293149119973622969239858152417678164812112068608
>
> ~$ seq 1024|awk 'BEGIN{i=1}{i*=2}END{print i+2}'
> inf
Note that there are well-known algorithms to implement integers of
arbitrary size, regardless of what the underlying architecture supports
(google for "bignum"). However, in the case of gawk the reason is that gawk
internally uses floating point (ie, probably C's "double" type) to
represent all kinds of numeric data, including integers.
There are some well-known issues with that, the main one being loss of
precision (for example, in your code above, try printing i and i+1 at each
iteration, and you'll see that, at least from a certain point onwards, awk
thinks they're the same. On a system I can access now, that happens
somewhere between 2^52 and 2^53).
For the details see the gawk manual:
http://www.gnu.org/software/gawk/manual/gawk.html#Floating-Point-Issues
--
D.