問題描述
為什么 ColdFusion 中的數(shù)學(xué)運算似乎不受浮點數(shù)學(xué)問題的影響?拿下代碼:
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
然而,當(dāng)添加兩個雙精度時,等效的 Java 代碼會產(chǎn)生我所期望的結(jié)果:
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 看到的,因為浮動數(shù)學(xué)的現(xiàn)實(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 是在幕后做了一些魔術(shù)"還是我在這里看到了一個孤立的異常?
Does ColdFusion do some "magic" behind the scenes or am I looking at an isolated anomaly here?
推薦答案
我強烈懷疑它只是在 輸出 上舍入不同.換句話說,問題仍然存在 - 當(dāng)使用 writedump
打印出這個特定值時,它恰好沒有出現(xiàn).
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
.
如果你使用會發(fā)生什么:
What happens if you use:
writedump(String.valueOf(result));
?
這篇關(guān)于為什么在 ColdFusion 中 0.06 + 0.01 = 0.07?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!