lua-users home
lua-l archive

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




On 09/05/16 12:35 PM, Loïc Le Cunff wrote:
Greetings,


Lately I've been messing around with this function, but sometimes my software (using results from lua scripts) would crash. I've isolated an example of this issue demonstrated in this simple script:
_____

a=600e-9
b=5e-9

N=a/b
N2=math.tointeger(N)

print(N)
print(N2)
print(math.tointeger(120.0))
_____

This returns:

120.0
nil
120

At first I thought it could be related to Windows (stuff built with TDM-GCC 5.1), but I could also replicated it on a Mint virtual machine (gcc 4.8.4).
I've seen it both in lua 5.3.0 and 5.3.2.
Note that more often than not math.tointeger behaves properly, but in this particular case it just doesn't.

Can anyone replicate this or is the issue only on my side?

Regards.
Pick a rounding behaviour. math.tointeger only converts to integer if x % 1.0 == 0.0.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.