How to check for -1. # IND (undefined) in Lua?

Incorrect substantiation of the issue :

I get a Lua format error message:

integer overflow trying to keep -1. # IND

The variable type(n) really number , and I can format it as a string (i.e. %s ), but it is not a number, for example:

 print(string.format("value=%s, type=%s", n, type(n))); 

for value NaN returns:

value = -1. # IND, type = number

I want to fix this, but I have no idea who generates this NaN (Lua has no debugger).

So all I have to do is reset a lot of asserts all over the code, until I can bind this discontinuous NaN value to the source.

But I can not find any condition that traps him, and Lua does not have isnan(x) .

Question

How can I check the number for -1.#IND in Lua?

Update

I tried:

 if (n ~= n) then print(string.format("NaN: value=%s, type=%s", n, type(n))); else print(string.format("value=%s, type=%s", n, type(n))); end; 

and he prints

value = -1. # IND, number

Update two . Just in case, I missed something, my actual code:

  if (oldValue ~= oldValue) then print(string.format("Is NaN: labelNumber=%d, formatString=\"%s\", oldValue=%s (%s)", labelNumber or 0, formatString or "nil", oldValue or "nil", type(oldValue))); else print(string.format("Is not NaN: labelNumber=%d, formatString=\"%s\", oldValue=%s (%s)", labelNumber or 0, formatString or "nil", oldValue or "nil", type(oldValue))); end; 

And the outputs of the faulty value:

Not NaN: labelNumber = 4, formatString = "% d", oldValue = -1. # IND (number)

Update three

While trying to solve this problem, I just noticed the absurdity of reality:

 function isnan(x) if type(x) ~= "number" then return false; --only a number can not be a number end; ... end; 
+7
source share
3 answers

n ~= n may work (with the caveats described in Mud's answer), but may be more portable:

function isnan(n) return tostring(n) == tostring(0/0) end

Those who are concerned about dividing by zero (as in Jan's comment, although I have not seen it in practice) can use an alternative version:

function isnan(n) return tostring(n) == tostring((-1)^.5) end

Full function:

 --local nanString = (tostring((-1) ^ 0.5)); --sqrt(-1) is also NaN. --Unfortunately, -- tostring((-1)^0.5)) = "-1.#IND" -- x = tostring((-1)^0.5)) = "0" --With this bug in LUA we can't use this optimization local function isnan(x) if (x ~= x) then --print(string.format("NaN: %s ~= %s", x, x)); return true; --only NaNs will have the property of not being equal to themselves end; --but not all NaN will have the property of not being equal to themselves --only a number can not be a number if type(x) ~= "number" then return false; end; --fails in cultures other than en-US, and sometimes fails in enUS depending on the compiler -- if tostring(x) == "-1.#IND" then --Slower, but works around the three above bugs in LUA if tostring(x) == tostring((-1)^0.5) then --print("NaN: x = sqrt(-1)"); return true; end; --i really can't help you anymore. --You're just going to have to live with the exception return false; end 
+4
source

Lua does not have isnan (x).

You can add it to your Lua host or create a module with this function. Just a few lines of code.

How can I check the number for -1. # IND in Lua?

Well, you know that it converts NaN to the string representation of '-1.#IND' , so you can write:

 function isnan(n) return tostring(n) == '-1.#IND' end 

Or depending on the platform, compiler, compiler settings, etc. This will work:

 function isnan(n) return n ~= n end 
+1
source

For serializing goals, this works best for me:

 local function isnan(val) if val==1/0 then return "1/0" elseif val==-1/0 then return "-1/0" elseif val~=val then return "0/0" end end 

This allows me to:

 print(v .. " = " .. isnan(val) or val) 

The result is, for example,

 { foo = 1/0, bar = 0/0, bla = -1/0, } 
0
source

All Articles