为什么CLng产生不同的结果? [英] Why does CLng produce different results?
问题描述
这是我的VBE(MS Excel 2007 VBA)一个小宝石:
?clng(150 * 0.85)
127
x = 150 * 0.85
?clng(x)
128
任何人都可以解释这个行为吗? IMHO第一个表达式应该产生128(.5舍入到最接近的偶数),或者至少应该两个结果相等。
在语句 clng(150 * 0.85)
, 150 * 0.85
以扩展精度计算:
150 = 1.001011 x 2 ^ 7
0.85
double precision =
1.1011001100110011001100110011001100110011001100110011 x 2 ^ -1
手动将这些数字相乘,即可得到
1.1111110111111111111111111111111111111111111111111111111001 x 2 ^ 6 =
127.4999999999999966693309261245303787291041245303787291049957275390625
这是59位,适合扩展精度。 $ x = 150 * 0.85
,该59位值舍入为53位,给出 127.5
1.1111111 x 2 ^ 6 = 1111111.1 = 127.5
因此它根据round-half-to-even舍入。
(请参阅我的文章 http: //www.exploringbinary.com/when-doubles-dont-behave-like-doubles/ 了解详情。)
Here's a little gem directly from my VBE (MS Excel 2007 VBA):
?clng(150*0.85)
127
x = 150*0.85
?clng(x)
128
Can anybody explain this behaviour? IMHO the first expression should yield 128 (.5 rounded to nearest even), or at least should both results be equal.
I think wqw is right, but I'll give the details.
In the statement clng(150 * 0.85)
, 150 * 0.85
is calculated in extended-precision:
150 = 1.001011 x 2^7
0.85
in double precision =
1.1011001100110011001100110011001100110011001100110011 x 2^-1
Multiply these by hand and you get
1.1111110111111111111111111111111111111111111111111111110001 x 2^6 =
127.4999999999999966693309261245303787291049957275390625
That's 59 bits, which fits comfortably in extended-precision. It's less than 127.5
so rounds down.
In the statement x = 150 * 0.85
, that 59 bit value is rounded to 53 bits, giving
1.1111111 x 2^6 = 1111111.1 = 127.5
So it rounds up according to round-half-to-even.
(See my article http://www.exploringbinary.com/when-doubles-dont-behave-like-doubles/ for more information.)
这篇关于为什么CLng产生不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!