[英]How the explicit cast from double to int rounds in Java?
System.out.println((int)(99.9999999999999));
returns 99 返回99
System.out.println((int)(99.999999999999999999999999999999999999999));
returns 100 返回100
Can you explain me why? 你能解释一下为什么吗?
The double
literal 99.9999999999999
can be represented as a double
which is less than 100
, so the cast to int
truncates the decimal part and 99
is the result. double
字面值99.9999999999999
可以表示为小于100
的double
,因此转换为int
截断小数部分, 99
就是结果。
The double
literal 99.999999999999999999999999999999999999999
has the closest actual value of 100
, and casting to int
is simply 100
here. double
literal 99.999999999999999999999999999999999999999
具有最接近的实际值100
,并且此处的int
为100
。
System.out.println(99.9999999999999);
System.out.println((int)(99.9999999999999));
System.out.println(99.9999999999999 == 100.0);
System.out.println(99.999999999999999999999999999999999999999);
System.out.println((int)(99.999999999999999999999999999999999999999));
System.out.println(99.999999999999999999999999999999999999999 == 100.0);
I used 100.0
as a double
literal here, to compare 2 double
values to see if they are the exact same double
value. 我在这里使用
100.0
作为double
字面值来比较2个double
值,看它们是否是完全相同的double
值。
This prints out: 打印出:
99.9999999999999
99
false
100.0
100
true
The JLS, Section 3.10.2 covers floating point literals: JLS,第3.10.2节涵盖了浮点文字:
The details of proper input conversion from a Unicode string representation of a floating-point number to the internal IEEE 754 binary floating-point representation are described for the methods valueOf of class Float and class Double of the package java.lang.
有关从浮点数的Unicode字符串表示到内部IEEE 754二进制浮点表示的正确输入转换的详细信息,请参阅类Float的方法valueOf和包java.lang的类Double。
And Double.valueOf
javadocs state: 和
Double.valueOf
javadocs状态:
Otherwise, s is regarded as representing an exact decimal value in the usual "computerized scientific notation" or as an exact hexadecimal value;
否则,s被认为代表通常的“计算机化科学记数法”中的精确十进制值或者精确的十六进制值; this exact numerical value is then conceptually converted to an "infinitely precise" binary value that is then rounded to type double by the usual round-to-nearest rule of IEEE 754 floating-point arithmetic, which includes preserving the sign of a zero value.
然后,这个精确的数值在概念上被转换为“无限精确”的二进制值,然后通过IEEE 754浮点运算的通常的舍入到最接近的规则舍入为double类型 ,其中包括保留零值的符号。
(emphasis mine) (强调我的)
That is, the value is rounded to produce the actual double
value. 也就是说,该值被舍入以产生实际的
double
值。
Parsing a floating point value occurs at compile time. 在编译时解析浮点值。 The javac compiler rounds the value to 100 before it ever emits any code into your .class file, because the most accurate representation of the number you wrote is not 99.9999999999 but 100: You're .0000000000099999999 closer to 100 than you are to 99.999999999 (I was probably inconsistent with my number of digits there).
javac编译器在将任何代码发送到.class文件之前将该值舍入为100,因为您编写的数字的最准确表示形式不是99.9999999999而是100:您的.0000000000099999999接近100而不是99.999999999(我可能与我的数字位数不一致)。 I wrote something that might help you on the .NET Code Generation blog .
我在.NET代码生成博客上写了一些可能对你有帮助的东西。 Nothing I wrote applies any differently to Java than it does to C# (or C++ or any other language that uses floating point)
我写的任何内容都不同于对C#(或C ++或任何其他使用浮点的语言)的应用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.