Normal lua:
print(tonumber('nan')) --> nil
Roblox lua:
print(tonumber('nan')) --> nan
Expected behavior
I expect it to return nil, just like it does in normal lua.
Normal lua:
print(tonumber('nan')) --> nil
Roblox lua:
print(tonumber('nan')) --> nan
Expected behavior
I expect it to return nil, just like it does in normal lua.
Do you not think this is the better behavior?
Luau has different handling of NaN in a number of places throughout the math library too.
Well, it’s a function that explicitly turns a value into a number, and it’s returning a value that is explicitly not a number. It’s very counter-intuitive, and everyone I spoke with before posting this had no idea this behavior existed. It’s very much expected that tonumber will return either a number or nil, and it’s not expected that tonumber will parse words into numbers. Like, tonumber('one')
does not give you 1
.
This is consistent with Lua 5.1 builds when they are using C99 standard library. I think some Windows builds might return nil, this is a Linux build.
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> print(tonumber("nan"))
nan
This has been brought up before and we discussed that but changing that results in tostring sometimes returning strings that tonumber can’t parse, and that doesn’t seem right either.
(also, nan
is definitely a number if you ask type
)