Java double precision sum trouble(Java双精度求和麻烦)
问题描述
我想知道为什么会出现此错误.(这是Eclipse调试的显示日志)
I would like to know why I get this error. (this is Display log of Eclipse debug)
var
(double) 2.8
tot.getIva()
(java.lang.Double) 0.17
var+tot.get()
(double) 2.9699999999999998
我不明白为什么我没有得到简单的 2.97!
I can not understand why I did not get simply 2.97!
推荐答案
如果你想要 2.97
,你应该使用 BigDecimal
.
If you wanted 2.97
, you should have used BigDecimal
.
double
s 在 binary 中存储为分数,而不是十进制.例如,3.75
只是存储为 2^1 + 2^0 + 2^(-1) + 2^(-2)
.
double
s are stored as fractions in binary, not decimal. So 3.75
, for example, is just stored as 2^1 + 2^0 + 2^(-1) + 2^(-2)
.
2.8
和 0.17
不能完全表示为二进制分数,所以会有一些舍入误差.
2.8
and 0.17
cannot be represented exactly as binary fractions, so there's going to be some rounding error.
这篇文章可能对您也有帮助.
这篇关于Java双精度求和麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java双精度求和麻烦


基础教程推荐
- 多个组件的复杂布局 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 大摇大摆的枚举 2022-01-01