sin、cos、tan 和舍入误差 [英] sin, cos, tan and rounding error

查看:24
本文介绍了sin、cos、tan 和舍入误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C/C++ 进行一些三角函数计算,但遇到了舍入错误的问题.例如,在我的 Linux 系统上:

I'm doing some trigonometry calculations in C/C++ and am running into problems with rounding errors. For example, on my Linux system:

#include <stdio.h>
#include <math.h>

int main(int argc, char *argv[]) {
    printf("%e
", sin(M_PI));
    return 0;
}

这个程序给出以下输出:

This program gives the following output:

1.224647e-16

当正确答案当然是 0 时.

when the correct answer is of course 0.

在使用三角函数时,我预计会有多少舍入误差?我怎样才能最好地处理这个错误?我熟悉用于比较浮点数的 Units in Last Place 技术,来自 Bruce Dawson 的 比较浮点数,但这在这里似乎不起作用,因为 0 和 1.22e-16 相差很多 ULP.

How much rounding error can I expect when using trig functions? How can I best handle that error? I'm familiar with the Units in Last Place technique for comparing floating point numbers, from Bruce Dawson's Comparing Floating Point Numbers, but that doesn't seem to work here, since 0 and 1.22e-16 are quite a few ULPs apart.

推荐答案

IEEE double 存储 52 位尾数,隐式前导one" 形成 53 位数字.结果的低位错误因此约占数字比例的 1/2^53.你的输出是与 1.0 的顺序相同,所以结果正好是 1参与 10^16(因为 53*log(2)/log(10) == 15.9).

An IEEE double stores 52 bits of mantissa, with the "implicit leading one" forming a 53 bit number. An error in the bottom bit of a result therefore makes up about 1/2^53 of the scale of the numbers. Your output is of the same order as 1.0, so that comes out to just about exactly one part in 10^16 (because 53*log(2)/log(10) == 15.9).

所以是的.这大约是您可以预期的精度极限.我是不确定您使用的 ULP 技术是什么,但我怀疑您是用错了.

So yes. This is about the limit of the precision you can expect. I'm not sure what the ULP technique you're using is, but I suspect you're applying it wrong.

这篇关于sin、cos、tan 和舍入误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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