Java Double变量有奇怪的值 [英] Java Double variables have strange values
问题描述
可能重复:
浮点运算不能在Java中产生精确的结果
我正在做这个简单的划分,但我得到一个非常奇怪的输出:
double a = 60 / (1.2-1.1);
a => 600.0000000000008
当它应该是600。
提前感谢
754二进制双,我们需要以二进制表示方式考虑1.1和1.2:
1.2 = 0b1.001100110011001100110011001100110011001100110011001100110011 ...
1.1 = 0b1.000110011001100110011001100110011001100110011001100110011001 ...
请注意,我们需要无限多的位来准确地表示它们在二进制。双重只有53位的意义,我们必须把数字切断:
1.2 = 0b1.001100110011001100110011001100110011001100110011001100110011 ...
$因此,1.2 - 1.1是:
1.1 = 0b1.000110011001100110011001100110011001100110011001100110011001 ...
^ round from here
==>
1.2〜0b1.0011001100110011001100110011001100110011001100110011
(=恰好1.1999999999999999555910790149937383830547332763671875)
1.1〜0b1.0001100110011001100110011001100110011001100110011010
(=恰好1.100000000000000088817841970012523233890533447265625)
1.2〜0b1.0011001100110011001100110011001100110011001100110011
- 1.1〜0b1.0001100110011001100110011001100110011001100110011010
------------------------------------ ----------------------------
0b0.00011001100110011001100110011001100110011001100110010000
(=正好是0.09999999999999986677323704498121514916419982910156250000)
我们实际上可以计算出60 / 0.09999999999998667732370449812151491641998291015625,这给了s
600.0000000000007993605777301137740672368493927467455286920109359612256820927 ...
^第16个有效数字
匹配OP的结果
600.0000000000008
Possible Duplicate:
Floating point arithmetic not producing exact results in JavaI was doing this simple division but I get a very strange output:
double a = 60/(1.2-1.1);
a => 600.0000000000008
When it should be 600.
thanks in advance
解决方案In IEEE-754 binary double, we need to consider 1.1 and 1.2 in the binary representation:
1.2 = 0b1.001100110011001100110011001100110011001100110011001100110011... 1.1 = 0b1.000110011001100110011001100110011001100110011001100110011001...
note that we need infinitely many bits to represent them exactly in binary. double only has 53 bits of significance, we have to chop off the numbers:
1.2 = 0b1.001100110011001100110011001100110011001100110011001100110011... 1.1 = 0b1.000110011001100110011001100110011001100110011001100110011001... ^ round from here ==> 1.2 ~ 0b1.0011001100110011001100110011001100110011001100110011 (= exactly 1.1999999999999999555910790149937383830547332763671875) 1.1 ~ 0b1.0001100110011001100110011001100110011001100110011010 (= exactly 1.100000000000000088817841970012523233890533447265625)
Hence 1.2 - 1.1 is:
1.2 ~ 0b1.0011001100110011001100110011001100110011001100110011 - 1.1 ~ 0b1.0001100110011001100110011001100110011001100110011010 ———————————————————————————————————————————————————————————————— 0b0.00011001100110011001100110011001100110011001100110010000 (= exactly 0.09999999999999986677323704498121514916419982910156250000)
We can actually compute 60 / 0.0999999999999998667732370449812151491641998291015625 exactly, which gives
600.0000000000007993605777301137740672368493927467455286920109359612256820927... ^ 16th significant figure
that matches OP's result of
600.0000000000008
这篇关于Java Double变量有奇怪的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!