Hello,
I’m wondering if there is any method to check if the number is NaN or inf?
I tried the following to no avail:
if VALUE == "nan" then print("this is not a number") end ------------------------------ if VALUE == "inf" then print("this is infinite number") end
Is there such thing as:
math.isNan() or math.isINF() ?
Thanks for your help!
For seeing if a value is a number:
if type(value) == "number" then -- do something end
For inf, perhaps:
if value == math.huge then -- do something end
Thanks for the suggestion.
The check for inf works flawlessly, however for NaN it is not working as I expected.
It only check if the type is number which is fine if I’m checking against string, but it wouldn’t tell me its nan
Found out a post at: http://snippets.luacode.org/snippets/Test_for_NaN_75
function isnan(x) return x ~= x end
To check if the number is NaN (not a number)
if isnan(value) == true then -- do something end
Good point, my apologises. I kind of skimmed through your question before answering.
For seeing if a value is a number:
if type(value) == "number" then -- do something end
For inf, perhaps:
if value == math.huge then -- do something end
Thanks for the suggestion.
The check for inf works flawlessly, however for NaN it is not working as I expected.
It only check if the type is number which is fine if I’m checking against string, but it wouldn’t tell me its nan
Found out a post at: http://snippets.luacode.org/snippets/Test_for_NaN_75
function isnan(x) return x ~= x end
To check if the number is NaN (not a number)
if isnan(value) == true then -- do something end
Good point, my apologises. I kind of skimmed through your question before answering.