Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

java - 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. 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.

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;

Why did they need to write the suffix d to the first part of 0.0 in NaN definition?

Was this on purpose or by chance?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

According to the Oak language spec, the format of floating point literals were:

  • 2.0d or 2.0D double
  • 2.0f or 2.0F or 2.0 float

but this changed to the familiar Java way by Java version 1.0

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.

The change was perhaps made to make it consistent with C-like languages, where the lack of suffix means a double.

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.

(Props to Mark Rotteveel for the nudge to look up the Oak language spec).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...