NaN means “不是一个数字” and is basically a representation of a special floating point value in the IEE754浮点数 standard. 不 generally means that the value is something that cannot be expressed with a valid floating point number.
All major modern CPUs seem to follow IEEE 754 which specifies floating point formats, and NaNs, which are just special float values, are part of that standard.
Therefore, the concept will be the very similar across any language, including Java which just emits floating point code directly to the CPU.
nan 0x7fc00000 NaN
zero_div_zero 0x7fc00000 NaN
sqrt_negative 0xffc00000 NaN
log_negative 0xffc00000 NaN
inf_minus_inf 0x7fc00000 NaN
inf_times_zero 0x7fc00000 NaN
quiet_nan1 0x7fc00001 NaN
quiet_nan2 0x7fc00002 NaN
signaling_nan1 0x7fa00001 NaN
signaling_nan2 0x7fa00002 NaN
nan_minus 0xffc00000 NaN
positive_inf 0x7f800000 Infinity
negative_inf 0xff800000 -Infinity
one_div_zero 0x7f800000 Infinity
log_zero 0xff800000 -Infinity
从中我们学到了一些东西:
没有任何明显结果的奇怪的浮动操作给 NaN:
0.0f / 0.0f
sqrt(-1.0f)
log(-1.0f)
generate a NaN.
在 C 语言中,实际上可以通过 feenableexcept请求在这些操作上产生信号来检测它们,但是我不认为在 Java 中会暴露出来: 为什么整数除以01/0会产生错误,而浮点1/0.0会返回“ Inf”?
在正负无穷大极限上的奇怪操作给出的是 +-无穷大而不是 NaN
1.0f / 0.0f
log(0.0f)
0.0几乎属于这一类,但问题可能在于它可能是正无穷大,也可能是负无穷大,因此它被保留为 NaN。
如果 NaN 是浮动操作的输入,则输出也趋向于 NaN
NaN 0x7fc00000、 0x7fc00001、 0x7fc00002有几个可能的值,尽管 x86 _ 64似乎只生成 0x7fc00000。
NaN and infinity have similar binary representation.