What is Nan?

What is nan in terms of Lua? Does it equate to nil? If so why doesn't this code work:

1
2
3
4
5
6
7
8
g.alive = function( event )
  print("Am I Alive", g.status)
  if not g.status then
      print("error")
      g.status = true
  end
end 
Runtime:addEventListener("enterFrame", g.alive)

nan or NaN is usually 'Not a Number'. You can get these from illegal math operations like dividing by 0. It's not clear to me how you got nan from the code you pasted.

Oh duh, not a number, epic face palm. It's not clear to me either lol. Thanks for the response. I know now what to look for.

Is there a way to test for non numbers? I still have no clue where I am passing it in. I should only be having nil or a number.

Not really. Testing for NaN is actually really hard and would require platform/architecture specific tricks if it could be done at all. The best thing is to avoid creating them in the first place.

You could maybe try to use the tonumber() function.
If it can't convert the parameter to a number it'll return nil, and if it's already a number it will just return the number.

Just a guess here, but if g.status is a Boolean type and not a number it could be throwing off the print command where you use a comma to separate the string and g.status. You could try
print(type(g.status))
above line 2 to find out what type g.status is.

Also you could try this for line 2:
print("Am I Alive" .. g.status)
or
("Am I Alive" .. tostring(g.status))

And you could do a check for the correct type (not quite a check for nan but might be just as good). Something like (untested):

If tostring(type(g.status)) ~= "number" then
--do something to handle the wrong type
end

Thanks ingemar and XenonBl I will try those options. Thing is those variables are only set in two places and are numbers or nil so I am still confused as to what it could be. And what else is in Lua that's not a table, boolean, numbers, or string???

From http://www.lua.org/pil/2.html

There are eight basic types in Lua: nil, boolean, number, string, userdata, function, thread, and table.

I know at least half of those would print out something or throw an error for trying use in a string. I will have to look into the rest thanks.

I expect NaN to be a number type in Lua. You won't be able to distinguish it from any other number.

Something that's "Not a Number" (NaN) is a number type??

My head just exploded.

views:1715 update:2011/10/29 17:13:10
corona forums © 2003-2011