【发布时间】:2015-09-23 14:53:07
【问题描述】:
如何检查浮点变量是否为 NaN?例如
math.Log(1.0) // not NaN
math.Log(-1.0) // NaN
【问题讨论】:
如何检查浮点变量是否为 NaN?例如
math.Log(1.0) // not NaN
math.Log(-1.0) // NaN
【问题讨论】:
为此使用math.IsNaN(...):playground
【讨论】:
math.NaN() 不等于自身:play.golang.org/p/H_SziEAdCvc
使用math.IsNaN:
IsNaN 报告 f 是否为 IEEE 754 “非数字”值。
【讨论】:
正如 Nico 的评论中提到的,因为 NaN 被定义为 f != f,这就是 math.IsNaN 所做的一切。
See the src here.
所以,你可以检查一下:
if f != f {
fmt.Printf("%v is NaN", f)
}
编辑成一个独立的答案。
【讨论】: