为什么将这两个双数相加不能给出正确的答案? [英] Why adding these two double does not give correct answer?
本文介绍了为什么将这两个双数相加不能给出正确的答案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
在Java中使用Doubles保持精度
import static java.lang.System.out;
public class q2{
public static void main(String args[]){
double x=4.02, y=0.05;
out.println(x+y);
}
}
输出:
4.069999999999999
4.069999999999999
为什么输出那个.我以为会是4.07.请解释为什么在Java中会发生这种情况?
Why is it outputting the that. I thought it would be 4.07. Please explain why this happens in java ?
抱歉,问题标题不正确.我再没有比这更好的标题了
推荐答案
这是因为某些数字(例如0.1)无法用二进制浮点数精确表示.
That is because some numbers -- such as 0.1 -- cannot be represented exactly in binary floating-point.
考虑阅读以下文章:
这篇关于为什么将这两个双数相加不能给出正确的答案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文