Revision Difference
nan#560490
<cat>Dev.Lua</cat>
# NaN⤶
⤶
If <page>nil</page> was supposed to be a number, it would be <page>NaN</page>.
⤶
This value is generated from heinous math operations such as the following:⤶
⤶
```lua⤶
local NaN = 0 / 0⤶
```⤶
⤶
This is not to be confused with infinity (which for some reason is confused with).⤶
⤶
```lua⤶
local Infinity = math.huge or 1 / 0⤶
```<title>Concepts - Not a Number (NaN)</title>⤶
# Not a Number (NaN)
⤶
Not a Number (NaN) is a special value returned as the result of math operations that are logically or mathematically invalid. ⤶
One of the more common ways to create a NaN value is to divide zero by zero. ⤶
⤶
⤶
<example>⤶
<description>⤶
NaN has some qualities that may not be immediately obvious, but are important to know:⤶
</description>⤶
<code>⤶
-- Intentionally create a NaN value for testing⤶
local NaN = 0/0⤶
⤶
-- It is not self-similar⤶
-- This is the main way that NaN values are detected in lua⤶
-- Prints: false⤶
print( "Is NaN self-similar?", NaN == NaN )⤶
⤶
-- It isn't nil⤶
-- Prints: false⤶
print( "Is NaN nil?", NaN == nil )⤶
⤶
-- It's not positive⤶
-- Prints: false⤶
print( "Is NaN positive?", NaN > 0 )⤶
⤶
-- It's not negative⤶
-- Prints: false⤶
print( "Is NaN negative?", NaN < 0 )⤶
⤶
-- It's not zero⤶
-- Prints: false⤶
print( "Is NaN zero?", NaN == 0 )⤶
⤶
-- It's not true⤶
-- Prints: false⤶
print( "Is NaN true?", NaN == true )⤶
⤶
-- It's not false⤶
-- Prints: false⤶
print( "Is NaN false?", NaN == false )⤶
⤶
-- It's truthy⤶
-- Prints: NaN is truthy⤶
if NaN then⤶
print( "Is NaN truthy?", true )⤶
end⤶
⤶
-- Despite the name, it is a number⤶
-- Prints: true⤶
print( "Is NaN a number?", type( NaN ) == "number" )⤶
</code>⤶
</example>⤶
⤶
## Additional Info⤶
⤶
The NaN value originates from the [Floating-Point Number (float)](https://en.wikipedia.org/wiki/Single-precision_floating-point_format) standard used almost universally by software and hardware for non-integer numbers.⤶
⤶
⤶