lua-users home
lua-l archive

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


I ran into a 32/64 bit problem yesterday.  I need to compute a big
integer (basically by multiplying with 2^40), and I only need it in a
string representation (i.e. no further arithmetic on the result).  The
naive approach results in a floating point representation:
tostring(4095 * 2^40) => 4.5025001157427e+15

On 64 bit, the following works:
('%d'):format(4095*2^40) => 4502500115742720

but on 32 bit I get:
('%d'):format(4095*2^40) => -2147483648

I solved this by using BitNum.lua, but still have some questions:
1) Why is there no support for '%ld'?
2) Is it possible to detect this overflow somehow? Is there a MAXINT I
did not find?
3) Is there another way to solve my problem?


P.S.: This is what I currently do
require 'BigNum'
b = tostring( * 2^40)