String to number conversion produces incorrect values (at least for -1!!)
-- prints false :/ print(tonum("-1")==-1) -- prints 0xFFFF.0001 print(tostr(tonum("-1"),true)) |
Looks like you don't have to use the tonum() function. It even fails on Lua's internal auto-conversion of strings to numbers in expressions:
> ?tostr(0+"-1",true) 0xffff.0001 |
Also, it seems to do it for all negative numbers for both styles, all the way down to the nadir value of -32768:
> ?tostr(tonum("-32768"),true) 0x8000.0001 > ?tostr(0+"-32768",true) 0x8000.0001 |
So it's probably the built-in converter that's at fault. In fact, I wouldn't be surprised if tonum() looks like this internally:
function tonum(s) return 0+s end |
Good catch, surprised no one else ran into it before... or maybe they did and neither of us did a search of the forum. ;) But either way, it needs fixing.
Maybe I ran into that bug cause I am using my json parser left and right ;)
This tripped me up again recently. >.< I could've sworn I saw this posted already, but I guess it's not fixed yet...
[Please log in to post a comment]