Reference · Math

math.isnan

signature
math.isnan(num) -> bool

What it does

Whether a float is NaN, "not a number", as Python's math.isnan. NaN comes out of arithmetic that has no answer, inf - inf or float("nan"), and it is the one value that is not equal to itself, so x == x is false for it and math.isnan is the way to catch it.

Examples

let nan = float("nan")print(math.isnan(nan), math.isnan(1), math.isnan(2.5))print(nan == nan, math.isnan(nan))
output
true false falsefalse true

Edge cases

let inf = float("inf")print(math.isnan(inf), math.isnan(inf - inf), math.isnan(0.0 * inf), math.isnan(sqrt(-1)))
output
false true true true

Errors

An argument that is not a number is a runtime error, not an error value: null is refused rather than reported as NaN.

this one fails
print(math.isnan(null))
output
line 1: `math.isnan` expects a number as argument 1, got null

Chaining

In a pipeline the number comes first. Dropping the NaNs before a statistic:

print(sqrt(-1) | math.isnan)print([1.0, sqrt(-1), 3.0] | filter((x) => !math.isnan(x)) | stats.mean)
output
true2.0

See also

math.isfinite, math.isinf, math.isclose, sqrt

Thread
9Useful? Like it, or add to its thread below.

Thread 0 posts

On math.isnan(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo