简体   繁体   English

为什么Double.NaN = 0.0d / 0.0的定义中有“d”?

[英]Why is there a “d” in the definition of Double.NaN = 0.0d / 0.0?

I just came across the definition of NaN in Double.class . 我刚刚在Double.class遇到了NaN的定义。 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;

I know that according the Java specification these literals represent the same number: 0.0 , 0.0d , and 0.0D . 我知道,根据Java规范这些文字代表了同一个号码: 0.00.0d0.0D

Also for other constants, they did not use the 'd' suffix: 另外对于其他常量,他们没有使用'd'后缀:

public static final double POSITIVE_INFINITY = 1.0 / 0.0;
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;

Why did they need to write the suffix d to the first part of 0.0 in NaN definition? 为什么他们需要在NaN定义中将后缀d写入0.0的第一部分?

Was this on purpose or by chance? 这是故意的还是偶然的?

According to the Oak language spec , the format of floating point literals were: 根据Oak语言规范 ,浮点文字的格式为:

  • 2.0d or 2.0D double 2.0d或2.0D双倍
  • 2.0f or 2.0F or 2.0 float 2.0f或2.0F或2.0浮动

but this changed to the familiar Java way by Java version 1.0 但是这改变了Java版本1.0的熟悉的Java方式

A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; 如果浮点文字后缀为ASCII字母F或f,则浮点文字的类型为float; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d. 否则其类型为double,并且可以选择以ASCII字母D或d为后缀。

The change was perhaps made to make it consistent with C-like languages , where the lack of suffix means a double. 这种变化可能是为了使其与类C语言保持一致,缺少后缀意味着双重语言。

So the d appears to be an historical relic; 所以d似乎是一个历史遗迹; although, in the linked version of the Oak spec (which is "preliminary"), there is a margin note saying that NaN isn't implemented yet. 虽然,在Oak规范的链接版本(这是“初步”)中,有一个保证金说明NaN尚未实施。 Perhaps it was implemented in a slightly later version, and has remained the same forever after. 也许它是在稍后的版本中实现的,并且在之后永远保持不变。

(Props to Mark Rotteveel for the nudge to look up the Oak language spec). (道具向马克罗特维尔推动查找橡树语言规范)。

According to the language specification, 0.0 is the same as 0.0d 根据语言规范, 0.00.0d相同

JLS Section 3.10.2 describes this as follows: JLS第3.10.2节描述如下:

A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; 如果浮点文字后缀为ASCII字母F或f,则浮点文字的类型为float; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d 否则其类型为double,并且可以选择以ASCII字母D或d为后缀

One of the ways of making a literal number floating point, is by using a '.' 制作文字数字浮点的方法之一是使用'。' in the number. 在号码。

Why the writers use the d suffix is not known. 作者使用d后缀的原因尚不清楚。 But often this is to add clarity or reduce the amount of effort required from the reader when understanding this code. 但通常这是为了增加清晰度或减少读者在理解此代码时所需的工作量。 The rules might be known to some people, but not to everyone. 某些人可能知道这些规则,但不是每个人都知道。 The defaults could also be different for other languages. 其他语言的默认值也可能不同。 Hence in many cases writing a more verbose version is better. 因此,在许多情况下,编写更详细的版本会更好。

Another example of this is parentheses: 另一个例子是圆括号:

 double a = b + c * d;

vs VS

 double a = b + (c * d);

I prefer the one with parentheses, because it is easier to read (even if all programmers know they are equal). 我更喜欢带括号的那个,因为它更容易阅读(即使所有程序员都知道它们是相同的)。

There is no difference between 0.0 and 0.0d as Java takes doubles by default to represent floating point numbers. 0.00.0d之间没有区别,因为Java默认使用双精度来表示浮点数。

Still, the code is more readable as in many languages, float is the default as well as it was in Oak later evolved to become Java so it looks like historical issue. 尽管如此,代码在许多语言中更具可读性, float是默认的,而后来Oak中 演变为Java,因此它看起来像历史问题。

--Link found by Andy Turner. - 安迪特纳发现的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM