[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
**Subject**: **Lua 5.3: How to get integer from out of range float?**
**From**: Karel Tuma <kat@...>
**Date**: Wed, 06 Aug 2014 17:50:50 +0200

Using 5.3 with #define LUA_INT_INT:
return math.pow(2,31)>>0
stdin:1: number has no integer representation
stack traceback:
stdin:1: in main chunk
[C]: in ?
To get integer anyway, with wraparound as it should be, ie equivalent of
int32_t i = (int64_t)d in C, one can:
string.format("%.0f", math.pow(2,31))>>0
-2147483648
This gives the correct result, but feels somewhat kludgy taking the detour
via string. Does anyone know of a better way?
Use case: Mixing FP math and bitwise ops, for example SHA256 [1]
[1] https://gist.github.com/katlogic/1cfe0dee78fba51324b4