为什么2.3 - 1.2 = 1.0999? [英] Why 2.3 - 1.2 = 1.0999 ?

查看:68
本文介绍了为什么2.3 - 1.2 = 1.0999?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用 float double - 结果相同。请帮忙。

解决方案

这是生活中的事实, float s和 double s等(十进制 s更好)。



阅读本文了解原因: http://docs.oracle.com/cd/E19957-01/806-3568 /ncg_goldberg.html [ ^ ]


你好。

问题是Java使用 IEEE 754 [ ^ ]标准。考虑不要使用 float ,而是使用 double 。当使用浮点数时,他应该小心。

有关详细信息,请参阅每个计算机科学家应该知道的关于浮点运算的内容 [ ^ ]

至于你的问题......你应该使用一些圆形操作或在BigDecimal [ ^ ]或者类似的东西。

< pre lang =java> BigDecimal a = new BigDecimal( 2.3);
BigDecimal b = new BigDecimal( 1.2 );
BigDecimal c = a.subtract(b);



请关注此链接 [ ^ 对于其他情况。

祝你好运。


Tried with float and double - same result. Please help.

解决方案

It's a fact of life with floats and doubles etc. (decimals are better).

Read this for the why : http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html[^]


Hello.
An issue is Java uses IEEE 754[^] standard. Consider not to use float, use double instead. When one uses floating point numbers he should be careful.
For further details please see What Every Computer Scientist Should Know About Floating-Point Arithmetic[^]
As for your question... You should use some round operations or to manipulate values with help of BigDecimal[^] or something like that.

BigDecimal a = new BigDecimal("2.3");
BigDecimal b = new BigDecimal("1.2");
BigDecimal c = a.subtract(b);


Please follow this link[^] for other cases.
Good luck.


这篇关于为什么2.3 - 1.2 = 1.0999?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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