High integers in current (g
)awk
are oddly broken without -M
. It is easy to spot that BEGIN {print 2^1024}
yields inf
, whereas BEGIN {print 2^1023}
works. One would therefore assume that the maximum integer in this particular implementation is 21024 − 1. Yet this is not the case.
A simple experiment, based on the fact that 21024 − 1 = 21023 + 21022 + ⋯ + 21 + 20:
BEGIN {for (i = 1023; i >= 0; --i) sum += 2^i; print sum}
This^^^ yields infinity, surprisingly enough. So, at which point do we need to stop adding the powers of 2 in order to obtain a valid result? On my systems the limit appears to be 971 — try 970 and it sums to infinity.
BEGIN {for (i = 1023; i >= 971; --i) sum += 2^i; print sum}
This^^^ prints 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368
.
The value has a surprising property in awk
: Whatever you add to it, up to a certain number, does not change it any more. (Try to print (e.g.) sum + 3
.) Incrementing it (although it appears to remain unchanged, based on the print
output) beyond a certain threshold yields infinity, eventually. This is definitely a bug.
As for the original sum above (21023 + ⋯ + 2971), it is still correct in awk
. Things start to fall apart once you try to increase that sum further. For example (and surprisingly), this still yields the same result as above:
BEGIN {for (i = 1023; i >= 971; --i) sum += 2^i
for (i = 969; i >= 0; --i) sum += 2^i
print sum}
Checking both sums with Python is easy:
sum = 0
for i in range(971, 1024):
sum += 2**i
print(sum) # awk gets this right
for i in range(0, 970):
sum += 2**i
print(sum) # awk without -M gets this wrong
All in all, I think I will be setting -M
in awk
all the time from now on!