How can I check whether a float variable is NaN or not? e.g.
math.Log(1.0) // not NaN
math.Log(-1.0) // NaN
How can I check whether a float variable is NaN or not? e.g.
math.Log(1.0) // not NaN
math.Log(-1.0) // NaN
Use math.IsNaN(...)
for that: playground
Use math.IsNaN
:
IsNaN reports whether f is an IEEE 754 “not-a-number” value.
As mentioned in Nico's comment, because NaN is defined as f != f
, that's all that math.IsNaN
does.
See the src here.
So, you can just check:
if f != f {
fmt.Printf("%v is NaN", f)
}
Edited to make into a standalone answer.
© 2022 - 2024 — McMap. All rights reserved.
math.NaN()
is not equal to itself: play.golang.org/p/H_SziEAdCvc – Adagio