float数据类型与八进制数的问题
public class test2 {
static double f = 011e3;
public static void main(String[] args) {
System.out.println("double "+f);
}
}
发现打印出来了double 11000.0
各位大神,小弟想知道计算机运行的原理,是不是一开始计算机看到0就进行八进制数的判断,结果检测到e,发现这个不是八进制数,然后就忽略0,直接读取后面的数,读取double数据后,赋值给f。最后打印出double。
------解决方案--------------------这个应该是科学计数法破坏了八进制数的表示,因为有e了,八进制数不包括e,所以前面那个0不起作用了。
------解决方案--------------------
java 语言规范(jls)规定:
表示浮点类型数的字面量,只有10进制表示和16进制表示。(没有其他进制表示)。
在本例中,将011看作是10进制数了。
在楼主的另一个例子中,0x11e30,被解晰成10进制73264。
想用指数形式的16进制表示:
需要用2的幂的形式,符号不是e而是p或P.
0x11p30;
相当于10进制:17*2^30.
解释未必准确,楼主可看
《The Java Language Specification Java SE 7 Edition》
3.10.2 Floating-Point Literals
A floating-point literal has the following parts: a whole-number part, a decimal or
hexadecimal point (represented by an ASCII period character), a fraction part, an
exponent, and a type suffix.
A floating-point literal may be expressed in decimal (base 10) or hexadecimal (base
16).
For decimal floating-point literals, at least one digit (in either the whole number or
the fraction part) and either a decimal point, an exponent, or a float type suffix are
required. All other parts are optional. The exponent, if present, is indicated by the
ASCII letter e or E followed by an optionally signed integer.
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.
Underscores are allowed as separators between digits that denote the whole-number
part, and between digits that denote the fraction part, and between digits that denote
the exponent.
FloatingPointLiteral:
DecimalFloatingPointLiteral
HexadecimalFloatingPointLiteral
DecimalFloatingPointLiteral:
Digits . Digitsopt ExponentPartopt FloatTypeSuffixopt
. Digits ExponentPartopt FloatTypeSuffixopt
Digits ExponentPart FloatTypeSuffixopt
Digits ExponentPartopt FloatTypeSuffix
ExponentPart:
ExponentIndicator SignedInteger
ExponentIndicator: one of
e E
SignedInteger:
Signopt Digits
Sign: one of
+ -
FloatTypeSuffix: one of
f F d D
HexadecimalFloatingPointLiteral:
HexSignificand BinaryExponent FloatTypeSuffixopt
HexSignificand:
HexNumeral
HexNumeral .
0 x HexDigitsopt . HexDigits
0 X HexDigitsopt . HexDigits
BinaryExponent:
BinaryExponentIndicator SignedInteger
BinaryExponentIndicator:one of
p P
A floating-point literal is of type float if it is suffixed with an ASCII letter F or
f; otherwise its type is double and it can optionally be suffixed with an ASCII
letter D or d.
------解决方案--------------------八进制虽然是以0开头但是没有科学计数的表示形式,011e3只是个科学技数的表现形式忽略0的存在,知道了吗。