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.
doubles 在 binary 中存储为分数,而不是十进制.例如,3.75 只是存储为 2^1 + 2^0 + 2^(-1) + 2^(-2).
doubles 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双精度求和麻烦
基础教程推荐
- 存储 20 位数字的数据类型 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
