Why is 65.12 - 2 not equal to 63.12 in java? [duplicate]

南楼画角 提交于 2019-12-13 06:03:41

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/14063936/why-is-65-12-2-not-equal-to-63-12-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!