为什么65.12 - 2在java中不等于63.12? [英] Why is 65.12 - 2 not equal to 63.12 in java?

查看:200
本文介绍了为什么65.12 - 2在java中不等于63.12?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:


当我遇到一个奇怪的bug时,我最近正在做一个项目。

当从65.12中减去2时,值是大于to)63.12。

下面是简化代码:
System.out.println(65.12-2);

并在控制台输出:
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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆