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))
P#56241 2018-09-05 16:54 ( Edited 2018-11-01 16:23)

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

P#56246 2018-09-05 21:32 ( Edited 2018-09-06 02:47)

Maybe I ran into that bug cause I am using my json parser left and right ;)

P#56258 2018-09-06 04:44 ( Edited 2018-09-06 11:42)

This tripped me up again recently. >.< I could've sworn I saw this posted already, but I guess it's not fixed yet...

P#58622 2018-11-01 12:23 ( Edited 2018-11-01 16:23)

@zep

Is this fixed in 0.1.12?

P#59552 2018-12-02 00:42

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-19 14:39:29 | 0.007s | Q:17