为什么我们需要IEEE 754余数?
我刚刚阅读了该主题(尤其是最后的评论).
I just read this topic (especially the last comments).
然后我在想,为什么我们真正需要这个是剩下的.但是,似乎没有多少人在Google上"对它感兴趣……
Then I was wondering, why we actually need this was of giving the remainder. But it seems, that not many people "on google" were interested in that before...
如果您正在寻找自己想要的原因,那就是所谓的范围缩小"
If you're looking for reasons why you would want it, one is for what is known as "range reduction"
假设您要使用sind
函数来计算参数的正弦值(以度为单位).做到这一点的一种天真的方法是
Let's say you want sind
function for computing the sine of an argument in degrees. A naive way to do this would be
sind(x) = sin(x*pi/180)
但是,这里的pi
不是真正的无理数pi
,而是最接近pi
的浮点数.这会导致诸如sind(180) == 1.2246467991473532e-16
之类的问题,以及诸如此和此(还有很多) ).
However pi
here is not the true irrational number pi
, but instead the floating point number closest to pi
. This leads to things like sind(180) == 1.2246467991473532e-16
, and SO questions like this and this (and many, many more).
但是正弦是周期函数,所以如果我们计算
But sine is a periodic function, so if we compute
remainder(x,90.0)
我们得到一个在间隔[-45,45]上的值.请注意,0、90、180、270等正好变为0,并且乘以pi/180
仍为0.因此,使用带正负号的sin
或cos
,我们可以在这些值处获得准确的结果(以及如果您进行了一些基本的错误分析,则可以证明它还可以减少其他值的错误.
we get a value on the interval [-45,45]. Note that 0, 90, 180, 270, etc. become exactly 0, and multiplying by pi/180
is still 0. Therefore taking the appropriately signed sin
or cos
, we can get the exact result at these values (and if you do some basic error analysis, you can demonstrate that it also reduces the error at other values).
两个跟进点:
- 如何确定要使用哪个
sin
或cos
?嗯,这就是remquo
的目的. - 不幸的是,由于中间舍入的变化,这仍然不能准确给出
sind(30.0) == 0.5
.有多种方法可以解决此问题,例如请参阅 Julia库所做的. >
- How do you determine which
sin
orcos
to use? Well, that's whatremquo
is for. - Unfortunately, this still won't give
sind(30.0) == 0.5
exactly, due to the vagaries of intermediate rounding. There are ways to fix this, e.g. see what the Julia library does.