java - 我在 Java 中看到双重算术的意外和不受欢迎的行为
问题描述
好的,JavaSE 1.8 在 Eclipse 版本中运行应用程序:Oxygen.3a Release (4.7.3a) Build id:20180405-1200。简单的算术:
Double dblBalance; ( showing as 2072.41 in debug, but also running the app in the IDE)
Double dblAmountofAllocation; ( showing as 1900.0 at runtime )
dblBalance = dblBalance - dblAmountofAllocation; ( dblBalance shows 172.40999995 at runtime)
( my workaround - shouldn't be necessary)
dblBalance = Math.round(dblBalance * 100.0) / 100.0;
我还在从命令行编译和运行的测试用例中对此进行了测试。所以它是可重现的。它是 Java 运行时引擎吗?
非常困惑 - 我想知道这是否可以被其他人复制。大多数 Double 值的组合都没有这个问题 - 我需要坚持使用本机数据类型吗?
解决方案
推荐阅读
- matlab - 基于整数值将矩阵拆分为单独的二进制矩阵 - matlab
- java - 为什么java jdk transferSize只有8k?
- powershell - 工作流程在 foreach 并行中的长工作流程调用上返回不可预测的结果
- apache-kafka - 错误:分区 0 没有领导者。跳过获取偏移量
- python - 如何在python中反转星号三角形
- python - 根据不等式填充图像的区域
- html - 使用 CSS 网格创建马赛克样式网格
- angular - 动态添加属性到输入元素
- javascript - 无法将 mlab 连接到 now.sh
- angular - Angular:您在预期流的位置提供了一个无效对象。您可以提供 Observable、Promise、Array 或 Iterable