Garry's Mod Wiki

Concepts - Not a Number (NaN)

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.


NaN has some qualities that may not be immediately obvious, but are important to know:

-- 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" )

Additional Info

The NaN value originates from the Floating-Point Number (float) standard used almost universally by software and hardware for non-integer numbers.