简体   繁体   English

0xp0打印0.0(十六进制浮点数字)

[英]0xp0 prints 0.0 (Hexadecimal Floating Point Literal)

I just wonder why this compiles? 我只是想知道为什么这个编译? and what does it mean since it does compile? 它是什么意思,因为它编译?

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

OUTPUT: OUTPUT:

0.0

The JLS explains it: JLS解释说:

HexadecimalFloatingPointLiteral:
    HexSignificand BinaryExponent FloatTypeSuffixopt

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

BinaryExponent:
    BinaryExponentIndicator SignedInteger

BinaryExponentIndicator:one of
    p P

Based on the above, I would expect a mandatory .HexDigit before the p , though. 基于以上所述,我希望在p之前有一个强制的.HexDigit

It's a floating point hex literal. 这是一个浮点十六进制文字。

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. 指数由ASCII字母p或P表示,后跟可选的有符号整数。

See the specification here . 请参阅此处的规范。

Just for reference, here is how to manually convert the following to a decimal number: 仅供参考,以下是如何手动将以下内容转换为十进制数字:

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

在此输入图像描述

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

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