Skip to main content
 首页 » 编程设计

Java 十六进制浮点字面量歧义

2024年06月03日40jiqing9006

Java specification ,它说了以下内容:

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.

据我了解,指示字母的指数必须是 p 或 P 才能解决与十六进制数字 e 或 E 的歧义。为什么它说后缀指示类型(float vs double)是可选的,当使用它时会用同样是十六进制数字的字母d、D、f、F引入歧义?

请您参考如下方法:

根据grammar :

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

可选的 FloatTypeSuffix 必须 遵循十六进制 float 的强制性 BinaryExponent

如果添加fd 而没有BinaryExponent,则它是十进制 float 。