Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Check if number is NaN or inf
Started by siaukia Aug 14 2014 11:28 PM

- - - - -
3 replies to this topic
number value infinity nan
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

siaukia

[GLOBAL: userInfoPane.html]
siaukia
  • Observer

  • 2 posts
  • Corona SDK

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!



[TOPIC: post.html]
#2

Gremlin Interactive

[GLOBAL: userInfoPane.html]
Gremlin Interactive
  • Contributor

  • 700 posts
  • Enterprise

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


[TOPIC: post.html]
#3

siaukia

[GLOBAL: userInfoPane.html]
siaukia
  • Observer

  • 2 posts
  • Corona SDK

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


[TOPIC: post.html]
#4

Gremlin Interactive

[GLOBAL: userInfoPane.html]
Gremlin Interactive
  • Contributor

  • 700 posts
  • Enterprise

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.


[topic_controls]
[/topic_controls]