简体   繁体   English

Java十六进制浮点文字歧义

[英]Java Hexadecimal Floating Point Literal Ambiguity

In Java specification , it says the following: 在Java 规范中 ,它说如下:

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表示,后跟可选的有符号整数。

As I understand, the exponent indicating letter has to be p or P to resolve ambiguity with the hexadecimal digit e or E. Why does it then say that the suffix indicating type (float vs double) is optional, when using it would introduce ambiguity with letters d, D, f, F, which are also hexadecimal digits? 据我所知,指示字母的指数必须是p或P来解决十六进制数字e或E的歧义。为什么然后说明后缀指示类型(float vs double)是可选的,当使用它时会引入歧义字母d,D,f,F,它们也是十六进制数字?

According to the grammar : 根据语法

 DecimalFloatingPointLiteral: Digits . [Digits] [ExponentPart] [FloatTypeSuffix] . Digits [ExponentPart] [FloatTypeSuffix] Digits ExponentPart [FloatTypeSuffix] Digits [ExponentPart] FloatTypeSuffix HexadecimalFloatingPointLiteral: HexSignificand BinaryExponent [FloatTypeSuffix] 

the optional FloatTypeSuffix must follow the mandatory BinaryExponent for a hex floating point number. 对于十六进制浮点数,可选的FloatTypeSuffix 必须遵循强制BinaryExponent

If you add an f or a d without a BinaryExponent , it's a decimal floating point number. 如果添加没有BinaryExponentfd ,则它是十进制浮点数。

The hexadecimal form is to make the mantissa precise. 十六进制形式是使尾数精确。 The exponent is a whole number and can be represented as a decimal precisely so doesn't have a conflict with letters d or f. 指数是一个整数,可以精确地表示为小数,因此不会与字母d或f冲突。

A normalised floating point number is of the form 标准化的浮点数是形式

sign * 1.mantissa * 2 ^ exponent

If you look at these examples, it is expected that the hexa-decimal notation is used when specifying the mantissa exactly. 如果你看一下这些例子,预计在准确指定尾数时会使用六进制十进制表示法。

// from java.lang.Double

public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308

public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308

public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324

In this form it is easy to see the expected mantissa and exponent. 在这种形式中,很容易看到预期的尾数和指数。 While other forms are possible they are not as clear. 虽然其他形式可能,但它们并不那么清楚。

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

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