Reference · Math
math.isnan
math.isnan(num) -> boolWhat 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))true false falsefalse trueEdge cases
- Infinity is not NaN.
inf - inf,0.0 * infandsqrt(-1)are NaN;math.sqrt(-1)is a runtime error instead, so it never gives one.0 / 0is a runtime error, division by zero, not NaN.
let inf = float("inf")print(math.isnan(inf), math.isnan(inf - inf), math.isnan(0.0 * inf), math.isnan(sqrt(-1)))false true true trueErrors
An argument that is not a number is a runtime error, not an error value: null is
refused rather than reported as NaN.
print(math.isnan(null))line 1: `math.isnan` expects a number as argument 1, got nullChaining
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)true2.0See also
math.isfinite, math.isinf, math.isclose, sqrt
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