P in constant declaration

后端 未结 4 1834
醉话见心
醉话见心 2020-12-09 01:46

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

public static final double MAX_VALUE = 0x1.fffffffffffffP+1023;
public stati         


        
相关标签:
4条回答
  • 2020-12-09 02:20

    It's used to signify a hexadecimal floating point literal.

    A floating-point literal has the following parts: a whole-number part, a decimal or hexadecimal point (represented by an ASCII period character), a fractional part, an exponent, and a type suffix. A floating point number may be written either as a decimal value or as a hexadecimal value. For decimal literals, the exponent, if present, is indicated by the ASCII letter e or E followed by an optionally signed integer. For hexadecimal literals, the exponent is always required and is indicated by the ASCII letter p or P followed by an optionally signed integer.

    From http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#230798

    0 讨论(0)
  • 2020-12-09 02:25

    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
    
    0 讨论(0)
  • 2020-12-09 02:30

    It's a hexadecimal floating-point literal, and the syntax is: sign*0x*significand*p*exponent. See this blog for an explanation.

    0 讨论(0)
  • 2020-12-09 02:34

    The p syntax if used for defining a double literal in hex. This is useful when you want to define its exact representation but isn't useful in general code because you want the double to be a decimal value rather than some hex pattern.

    0 讨论(0)
提交回复
热议问题