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

Categories

In java.lang.Double, there are the following constant declarations:

public static final double MAX_VALUE = 0x1.fffffffffffffP+1023;
public static final double MIN_NORMAL = 0x1.0p-1022;

What is the P for? Is the difference in case important?

I am aware of the L, D and F used for Longs, Doubles and Floats, but have never seen a P before.

See Question&Answers more detail:os

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

1 Answer

The P (or p) indicates a hexadecimal floating-point literal, where the significand is specified in hex.

The p is used instead of the e. The d and f suffixes that you've seen are orthogonal to this: both 0x1.0p+2f and 0x1.0p+2d are valid literals (one is of type float and the other is of type double).

At first glance it might seem that the 0x prefix is sufficient to identify a hex floating-point literal, so why have the Java designers chosen to change the letter from e to p? This has to do with e being a valid hex digit, so keeping it would give rise to parsing ambiguity. Consider:

0x1e+2

Is that a hex double or the sum of two integers, 0x1e and 2? When we change e to p, the ambiguity is resolved:

0x1p+2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...