为什么会有"d"字样?在Double.NaN的定义中= 0.0d/0.0? [英] Why is there a "d" in the definition of Double.NaN = 0.0d / 0.0?
问题描述
我刚刚在 Double.class
中遇到了 NaN
的定义.它说:
I just came across the definition of NaN
in Double.class
. It says:
/**
* 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
.
I know that according the Java specification these literals represent the same number: 0.0
, 0.0d
, and 0.0D
.
对于其他常量,它们也没有使用'd'后缀:
Also for other constants, they did not use the 'd' suffix:
public static final double POSITIVE_INFINITY = 1.0 / 0.0;
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
为什么他们需要在NaN定义中将后缀d写入0.0的第一部分?
Why did they need to write the suffix d to the first part of 0.0 in NaN definition?
这是故意还是偶然?
推荐答案
根据Oak语言规范,浮点文字的格式为:
According to the Oak language spec, the format of floating point literals were:
- 2.0d或2.0D double
- 2.0f或2.0F或2.0 float
,但是通过 Java版本1.0
如果浮点文字的后缀为ASCII字母F或f,则其类型为float;否则,其类型为double,并且可以选择在其后缀ASCII字母D或d.
A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d.
也许已进行了更改,以使其与类似于C的语言保持一致,缺少后缀则意味着加倍.
The change was perhaps made to make it consistent with C-like languages, where the lack of suffix means a double.
所以 d
似乎是历史遗物;不过,在Oak规范的链接版本(初步")中,有一个注解指出NaN尚未实现.也许它是在稍晚的版本中实现的,此后一直保持不变.
So the d
appears to be an historical relic; although, in the linked version of the Oak spec (which is "preliminary"), there is a margin note saying that NaN isn't implemented yet. Perhaps it was implemented in a slightly later version, and has remained the same forever after.
(提示标记Rotteveel 以推动查找Oak语言规范).
(Props to Mark Rotteveel for the nudge to look up the Oak language spec).
这篇关于为什么会有"d"字样?在Double.NaN的定义中= 0.0d/0.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!