浮点数不是减法后的预期数 [英] float number is not the expected number after subtraction
本文介绍了浮点数不是减法后的预期数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下声明:
float diff = tempVal - m_constraint.getMinVal();
tempVal
被声明为浮点数 getMinVal()
返回浮点值。
tempVal
is declared as a float and the getMinVal()
returns a float value.
我打印出以下内容:
diff = 0.099999905,tempVal = 5.1,m_constraint.getMinVal()= 5.0
我期待 diff 0.1 但不是上述数字。怎么做?
I expect the diff is 0.1 but not the above number. how to do that?
推荐答案
Floats使用 IEEE754
来表示数字,该系统存在一些舍入错误。
Floats use the IEEE754
to represent numbers, and that system has some rounding errors.
如果你在做算术并且需要准确的话,那就是底线使用 float
或 double
但我们 BigDecimal
Bottom-line if you are doing arithmetic and it needs to be exact don't use float
or double
but us BigDecimal
这篇关于浮点数不是减法后的预期数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文