如何在 golang 中检查 NaN

How to check for NaN in golang

如何检查浮点变量是否为 NaN?例如

math.Log(1.0) // not NaN
math.Log(-1.0)  // NaN

为此使用 math.IsNaN(...)playground

使用math.IsNaN:

IsNaN reports whether f is an IEEE 754 “not-a-number” value.

正如 Nico 的评论中提到的,因为 NaN 被定义为 f != f,所以 math.IsNaN 所做的就是这些。 See the src here.

所以,您可以检查一下:

if f != f {
    fmt.Printf("%v is NaN", f)
}

编辑成一个独立的答案。