2012-07-25 12 views
17

Me pregunto por qué esta compila? y ¿qué significa ya que compila?0xp0 imprime 0.0 (Literal punto hexadecimal flotante)

System.out.println(0xp0); // p? 

SALIDA:

0.0 
+0

Para mí esto no compila, con 'javac 1.7.0_02'. Solo '0x0p0' lo hace. –

+0

@TheGuyOfDoom Estoy usando '1.7.0_05'. –

+0

Estoy por cierto en el Sun JDK. eso podría ser relevante. –

Respuesta

9

Es un literal hexadecimal de coma flotante.

For hexadecimal floating-point literals, at least one digit is required (in either the whole number or the fraction part), and the exponent is mandatory, and the float type suffix is optional. The exponent is indicated by the ASCII letter p or P followed by an optionally signed integer.

Consulte la especificación here.

+0

+1 Bueno, la primera vez que sé sobre el punto flotante hex. –

11

The JLS lo explica:

HexadecimalFloatingPointLiteral: 
    HexSignificand BinaryExponent FloatTypeSuffixopt 

HexSignificand: 
    HexNumeral 
    HexNumeral . 
    0 x HexDigitsopt . HexDigits 
    0 X HexDigitsopt . HexDigits 

BinaryExponent: 
    BinaryExponentIndicator SignedInteger 

BinaryExponentIndicator:one of 
    p P 

base en lo anterior, yo esperaría un sistema obligatorio de .HexDigit antes de la p, sin embargo.

0

Sólo como referencia, aquí es cómo convertir manualmente el siguiente para un número decimal:

double hfpl = 0x1e.18p4; 
System.out.println(hfpl); // 481.5 

enter image description here

Cuestiones relacionadas