luvit/luvit

Losing accuracy at 64 bits.

hansonry opened this issue · 2 comments

When entering the following command into the luvit repl
print(string.format("%x",0x3736353433323130))

I would expect to get 3736353433323130 but instead I get 3736353433323200

If I run the same command on https://www.lua.org/cgi-bin/demo I get the correct result.

As a side note, is there a way to figure out if 64 bit support is built into luvit?

The prebuilt luvit binaries use luajit, which is a fork of Lua 5.1, which only has one number type: double precision floating point (a double). Doubles can only represent the integer range between -2^52 and 2^52, everything beyond that loses precision.

If you want to use 64 bit numbers you'll need to use luajit's native integer stuff that is provided via it's ffi library. The easiest way to get one of those is the number suffixes, for example: 12ULL is 12, but as a unsigned long long, which is 64 bits.

To add to that: Native integer support was added with 5.3 and remains in the current 5.4, the version that the online demo uses.