Reference · Math

math.isfinite

signature
math.isfinite(num) -> bool

What it does

Whether a number is neither infinite nor NaN, as Python's math.isfinite. Every int is finite; a float is finite unless arithmetic ran past the largest float or had no answer. It is the test to run before storing a computed float or sending it as JSON.

Examples

print(math.isfinite(1), math.isfinite(2.5), math.isfinite(1e308))print(math.isfinite(float("inf")), math.isfinite(float("nan")))
output
true true truefalse false

Edge cases

print(math.isfinite(9223372036854775807), math.isfinite(-0.0))print(math.isfinite(1e308 * 10), math.isfinite(float("inf") - float("inf")))
output
true truefalse false

Errors

An argument that is not a number is a runtime error, not an error value: text, null and a decimal are refused rather than reported as not finite.

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

Chaining

In a pipeline the number comes first. Dropping the values that cannot be charted:

print(1e308 * 10 | math.isfinite)print([1.5, float("inf"), 2.5, float("nan")] | filter(math.isfinite))
output
false[1.5, 2.5]

See also

math.isinf, math.isnan, math.isclose, float

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

Thread 0 posts

On math.isfinite(): 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