Why does 0.06 + 0.01 = 0.07 in ColdFusion?(为什么在 ColdFusion 中 0.06 + 0.01 = 0.07?)
问题描述
为什么 ColdFusion 中的数学运算似乎不受浮点数学问题的影响?拿下代码:
Why don't math operations in ColdFusion seem to be affected by floating point math issues? Take the code:
result = 0.06 + 0.01;
writedump(result);
writedump(result.getClass().getName());
哪些输出
0.07
java.lang.Double
java.lang.Double
然而,当添加两个双精度时,等效的 Java 代码会产生我所期望的结果:
However the equivlant Java code produces what I"d expect when adding two doubles:
public static void main(String[] args) {
double a = 0.01d;
double b = 0.06d;
System.out.println(a + b); //0.06999999999999999
}
这是我期望从 ColdFusion 看到的,因为浮动数学的现实(http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html).
This is what I'd expect to see from ColdFusion because of the realities of floating math (http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html).
ColdFusion 是在幕后做了一些魔术"还是我在这里看到了一个孤立的异常?
Does ColdFusion do some "magic" behind the scenes or am I looking at an isolated anomaly here?
推荐答案
我强烈怀疑它只是在 输出 上舍入不同.换句话说,问题仍然存在 - 当使用 writedump
打印出这个特定值时,它恰好没有出现.
I strongly suspect it's simply rounding differently on output. In other words, the issue is still there - it just happens not to show up when this particular value is printed out with writedump
.
如果你使用会发生什么:
What happens if you use:
writedump(String.valueOf(result));
?
这篇关于为什么在 ColdFusion 中 0.06 + 0.01 = 0.07?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么在 ColdFusion 中 0.06 + 0.01 = 0.07?


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