Log In  


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))
1


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.

@zep


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...


@zep

Is this fixed in 0.1.12?



[Please log in to post a comment]