四舍五入的奇数-"100"的特殊之处是什么? [英] Rounding oddity - what is special about "100"?

查看:111
本文介绍了四舍五入的奇数-"100"的特殊之处是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人对haskell(GHCi,7.2.1版)中的这种奇怪的舍入有解释吗?除非我乘以100,否则一切似乎都很好.

Does anyone have an explanation for this strange rounding in haskell (GHCi, version 7.2.1). Everything seems fine unless I multiply with 100.

*Main> 1.1 
1.1

*Main> 1.1 *10
11.0

*Main> 1.1 *100
110.00000000000001

*Main> 1.1 *1000
1100.0

*Main> 1.1 *10000
11000.0

令我感到困惑的是,仅当乘以100时才显示舍入误差.

what is puzzeling me is that the rounding error only shows when multiplying with 100.

Edit(2):我收到的评论使我意识到,这与haskell完全无关,而是一个浮点数的普遍问题.关于浮点数奇数问题,已经问了很多问题(并得到了回答),其中不讨好的问题通常是将浮点数与实数混淆.

Edit(2): The comments I received made me realize, that this it totally unrelated to haskell, but a general issue with floating point numbers. Numerous questions were already asked (and answered) about floating-point number oddities, where the undelying issue typcally was confusing floats with real numbers.

Perl,python,javascript和C均报告1.1 * 100.0 = 110.00000000000001.这是C要做的

Perl, python, javascript and C all report 1.1 * 100.0 = 110.00000000000001. Here is what C does

double     10.0 * 1.1 = 11.000000000000000000000000
double    100.0 * 1.1 = 110.000000000000014210854715
double          110.0 = 110.000000000000000000000000
double   1000.0 * 1.1 = 1100.000000000000000000000000

为什么只有在乘以100时才会发生这种情况"(即使有一个精确的表示为110.0的问题)仍然没有答案,但是我想除了完全逐步执行浮点乘法之外,没有简单的答案. (感谢Dax Fohl强调二进制中10没什么特别的)

The question "why does this happen only when multiplying with 100" (even though there is a precise representation for 110.0) is still unanswered, but I suppose there is no simple answer, other than fully stepping through a floating-point multiplication (Thanks to Dax Fohl for stressing that 10 is nothing special in binary)

推荐答案

为什么只有在乘以100时才会发生这种情况"(即使有一个精确的表示为110.0的问题)仍然没有答案,但是我想除了完全逐步执行浮点乘法之外,没有简单的答案.

The question "why does this happen only when multiplying with 100" (even though there is a precise representation for 110.0) is still unanswered, but I suppose there is no simple answer, other than fully stepping through a floating-point multiplication

好吧,我认为在假定IEEE 754算术和(默认)四舍五入取整模式的前提下,不用写二进制乘法的长度,我可能会说些什么.

Well, I think there may be things one can say without going to the length of writing the binary multiplication, assuming IEEE 754 arithmetic and the (default) round-to-nearest rounding mode.

双精度1.1d是实数1.1的 ULP 的一半.当您将其乘以10、100、1000以及几的10的幂时,您将乘以N可以精确表示为双精度的数字N,并具有实数1.1 * N的结果可以精确表示的附加属性也是双人这使得1.1 * N成为浮点乘法结果的良好候选者,我们将其写为RN(N * 1.1d).但是乘法仍然不会自动舍入为1.1 * N:

The double 1.1d is half a ULP from the real number 1.1. When you multiply it by 10, 100, 1000, and a few more powers of ten, you multiply by a number N that is exactly representable as a double, with the additional property that the result of the real multiplication 1.1 * N is exactly representable as a double, too. That makes 1.1 * N a good candidate for the result of the floating-point multiplication, which we'll write RN(N * 1.1d). But still the multiplication is not automatically rounded to 1.1 * N:

RN(N * 1.1d) = N * 1.1d + E1 with |E1| <= 0.5 * ULP(N*1.1d)

             = N * (1.1 + E2) + E1 with |E2| <= 0.5 * ULP(1.1)

             = N * 1.1 + (N * E2 + E1)

现在的问题是| N * E2 + E1 |与ULP(N * 1.1d)相比,因为我们假设N * 1.1恰好是浮点数,所以乘法的结果(也是浮点数)在N * 1.1的1 ULP之内,必须为N * 1.1.

And the question now is how |N * E2 + E1| compares to ULP(N*1.1d), because since we have assumed N * 1.1 is exactly a floating-point number, if the result of the multiplication (which is also a floating-point number) is within 1 ULP of N * 1.1, it has to be N * 1.1.

简而言之,关于100的特殊之处并没有那么多……对于真实的1.1d * 100而言,特殊之处不大,其中1)接近2的幂次方,而低于2的幂次方; 2)的误差为与将实数1.1转换为double时的错误相同的符号.

In short, it is not so much what's special about 100… It is what's special about the real 1.1d * 100, which 1) is close to a power of two while being below it and 2) has an error of the same sign as the error when converting the real 1.1 to double.

每当实数N * 1.1d相对于1.1比1更接近最接近的次幂2时,将1.1d乘以N的浮点数的结果必须恰好是N * 1.1(我认为) .这种情况的一个示例是N = 1000,N * 1.1d〜1100,刚好在1024以上.

Everytime the real N * 1.1d is relatively closer to the nearest inferior power of two than 1.1 is to 1, the result of the floating-point multiplication of 1.1d by N has to be exactly N * 1.1 (I think). An example of this case is N=1000, N*1.1d ~ 1100, just above 1024.

当实数N * 1.1d相对于2的直接乘方幂比1.1接近2时,可能存在一个比N * 1.1更好的表示N * 1.1d的浮点数.但是,如果误差E1和E2相互补偿(即符号相反),则不应发生这种情况.

When the real N * 1.1d is relatively closer to the immediately superior power of two than 1.1 is to 2, there may be a floating-point number that represents N * 1.1d better than N * 1.1 does. But if the errors E1 and E2 compensate each other (i.e. have opposite signs), this should not happen.

这篇关于四舍五入的奇数-"100"的特殊之处是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆