Math.isFinite
func Math.isFinite(x: float64): boolMath.isFinite reports whether x is neither NaN nor an infinity.
Details
- Float arithmetic never panics: an overflow gives an infinity and
0.0 / 0.0givesNaN, soMath.isFiniteis how you check a result before you use it.
Basic examples
Math.isFinite(1.5) // true
Math.isFinite(Math.Infinity) // false
Math.isFinite(Math.NaN) // falseScope
Both infinities are not finite:
Math.isFinite(Math.Infinity) // false
Math.isFinite(-Math.Infinity) // falseZero of either sign, the largest finite value, and the smallest positive value are finite:
Math.isFinite(0.0) // true
Math.isFinite(-0.0) // true
Math.isFinite(Math.maxFloat64) // true
Math.isFinite(5e-324) // trueArithmetic and Math functions that give an infinity or NaN:
Math.isFinite(Math.maxFloat64 * 2.0) // false
Math.isFinite(1.0 / 0.0) // false
Math.isFinite(Math.log(0.0)) // false
Math.isFinite(Math.sqrt(-1.0)) // falseA float32 that overflows becomes an infinity, which stays one when converted to float64:
let big = float32(1e39)
Math.isFinite(float64(big)) // falsePass Math.isFinite as a function value, or call it in a lambda on a record field:
[1.0, Math.NaN, 2.0, -Math.Infinity].filter(Math.isFinite) // [1.0, 2.0]
type Reading = { sensor: string, value: float64 }
let readings = [Reading{ sensor: "a", value: 20.5 }, Reading{ sensor: "b", value: Math.NaN }]
readings.filter({ !Math.isFinite(x.value) }).map({ x.sensor }) // ["b"]In a pipe, the value is the argument:
2.0 | Math.isFinite() // trueApplications
Reject a parsed number that is not finite. String.toFloat reads "inf" and "NaN" as numbers:
func parseAmount(s: string): float64 | null {
let n = s.toFloat()
if n == null || !Math.isFinite(n) {
return null
}
n
}
parseAmount("12.5") // 12.5
parseAmount("inf") // null
parseAmount("NaN") // null
parseAmount("abc") // nullAverage only the usable values of a series:
func mean(xs: Array[float64]): float64 | null {
let ok = xs.filter(Math.isFinite)
let total = ok.sum()
if total == null {
return null
}
total / float64(ok.length())
}
mean([1.0, Math.NaN, 3.0, Math.Infinity]) // 2.0
mean([Math.NaN]) // nullWrite null in place of a value that is not finite before Json.stringify, which panics on NaN and the infinities:
func toJson(xs: Array[float64]): string {
let safe: Array[float64 | null] = xs.map({ if Math.isFinite(x) { x } else { null } })
Json.stringify(safe)
}
toJson([1.5, Math.NaN, Math.Infinity]) // "[1.5,null,null]"
Json.stringify([1.5, Math.NaN]) // panic: Json.stringify: NaN has no JSON formProperties and relations
A value is finite exactly when it is neither NaN nor an infinity, and then subtracting it from itself gives 0.0:
for v in [0.0, -0.0, 1.5, Math.maxFloat64, Math.NaN, Math.Infinity, -Math.Infinity] {
let viaNaN = !Math.isNaN(v) && v != Math.Infinity && v != -Math.Infinity
assert(Math.isFinite(v) == viaNaN)
assert(Math.isFinite(v) == (v - v == 0.0))
}A finite value with no fraction converts to int when it is in range; Math.toInt gives null for every value that is not finite:
Math.toInt(Math.Infinity) // null
Math.toInt(Math.NaN) // null
Math.toInt(42.0) // 42Possible issues
Comparing with Math.NaN does not find NaN, since NaN equals nothing, itself included. Use Math.isFinite or Math.isNaN:
let v = 0.0 / 0.0
v == Math.NaN // false
Math.isNaN(v) // true
Math.isFinite(v) // falseAn int argument is a compile error, since numeric types never mix:
let ok = Math.isFinite(1) // error: cannot use int where float64 is expectedSo is a float32; convert it with float64 first:
let f = float32(1.5)
let ok = Math.isFinite(f) // error: cannot use float32 where float64 is expectedNot finite is not the same as infinite: !Math.isFinite(v) is also true for NaN. To test only for an infinity, compare Math.abs with Math.Infinity:
!Math.isFinite(Math.NaN) // true
Math.abs(Math.NaN) == Math.Infinity // false
Math.abs(-Math.Infinity) == Math.Infinity // trueConverting a value that is not finite to an integer panics:
int(Math.Infinity) // panic: Infinity does not convert to intSee also
Math.isNaN · Math.toInt · Math.clamp · Math.abs · String.toFloat · Array.sum · Json.stringify · float
