为什么在 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));

?

相关文章