本文介绍了为什么在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());

哪些输出

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.

如果使用:

writedump(String.valueOf(result));

这篇关于为什么在ColdFusion中0.06 + 0.01 = 0.07?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!