【发布时间】:2017-09-26 15:19:31
【问题描述】:
我刚刚在Double.class 中遇到了NaN 的定义。它说:
/**
* A constant holding a Not-a-Number (NaN) value of type
* {@code double}. It is equivalent to the value returned by
* {@code Double.longBitsToDouble(0x7ff8000000000000L)}.
*/
public static final double NaN = 0.0d / 0.0;
我知道根据 Java 规范,这些文字代表相同的数字:0.0、0.0d 和 0.0D。
对于其他常量,它们也没有使用 'd' 后缀:
public static final double POSITIVE_INFINITY = 1.0 / 0.0;
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
为什么他们需要在 NaN 定义的 0.0 的第一部分写后缀 d?
这是故意的还是偶然的?
【问题讨论】:
-
存在一种可能性,即它只是实施者的防御性编程。从 TODO 和/或注释掉的代码来看,JDK 并不是编程的黄金标准。
-
也完全有可能这只是一个历史文物(谁知道呢,也许在 Oak 中
0.0是一个浮点字面值)。 -
@MarkRotteveel 正确,根据this version of the Oak language spec:“2.0f or 2.0F or 2.0 float”。
标签: java