How to detect NaN

From MiniScript Wiki
Jump to navigation Jump to search

Invalid math operations may result in a numeric value of NaN ("Not a Number"). These values have special semantics: they are not considered equal to anything, even themselves. In fact NaN is the only value in MiniScript not equal to itself.

So if you need to find out whether a numeric value is NaN, just check whether it does not equal itself.

Example

x = 0/0
print x       // prints "nan" or "NaN", depending on the host platform
print x != x  // prints 1 (true)

This could of course be wrapped up in a convenience function as follows.

isNan = function(x)
    return x != x
end function