lua-users home
lua-l archive

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


On Thu, May 9, 2019 at 11:49 PM Sean Conner wrote:
It was thus said that the Great Egor Skriptunoff once stated:
>
> BTW, math.abs(1<<63) in both Lua 5.3 and 5.4 returns negative value.
> That's a bug.

  That's also a bug in most CPUs (those that are 2's complement), although a
number will set the overflow flag [1] to signal the condision [2].


CPUs are making all arithmetic modulo their word (2^32), this is how the CPU operations are declared in CPU architecture manual.
So, it's not a bug in CPU.

Integer operators in Lua are explicitly declared modulo 2^64 (Lua manual section 3.4.1 - Arithmetic operators), so the following is OK:
assert(-INT_MIN==INT_MIN)

But Lua math library is supposed to give mathematically correct results.
math.abs(INT_MIN) should return correct (float) value.
The similar issue has already been successfully solved in Lua: function tonumber returns result having most suitable numeric subtype.