为什么65.12 - 2在java中不等于63.12? [英] Why is 65.12 - 2 not equal to 63.12 in java?
问题描述
当我遇到一个奇怪的bug时,我最近正在做一个项目。
当从65.12中减去2时,值是大于to)63.12。
下面是简化代码: 并在控制台输出: 我不确定为什么会出现这种情况,如果有人知道一个简单的解决方法/解决方法,那就太好了! 谢谢。
System.out.println(65.12-2); $ c $
63.120000000000005
Possible Duplicate:
Floating point arithmetic not producing exact results in Java
I was recently working on a project when I came across a strange bug.
When 2 was subtracted from 65.12 the value was greater (not equal to) 63.12.
Here's the simplified code:
System.out.println(65.12-2);
And the output in the console:
63.120000000000005
I'm not sure why this is the case and if anyone knows a simple fix/workaround that would be great!
Thanks.
It has to do with the way floating-point values are handled by computers. The recommended text for fully understanding the topic is: What Every Computer Scientist Should Know About Floating-Point Arithmetic
这篇关于为什么65.12 - 2在java中不等于63.12?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!