The standard library does provide checking for NaN/inf, but under the generalized concept of classify
, which is why it is easy to miss. To illustrate:
import math
# Inf and NaN literals are defined in system.nim as
# Inf* {.magic: "Inf".} = 1.0 / 0.0
# NaN* {.magic: "NaN".} = 0.0 / 0.0
let floatsToTest = [0.0, Inf, NaN]
for x in floatsToTest:
echo x, " is NaN ", x.classify == fcNaN
echo x, " is inf ", x.classify == fcInf
Output:
0.0 is NaN false
0.0 is inf false
inf is NaN false
inf is inf true
nan is NaN true
nan is inf false
classify
can also test for other properties like fcSubnormal
, fcNegZero
, or fcNegInf
(see FloatClass
).