相关疑难解决方法(0)

sin,cos,tan和舍入误差

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

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

int main(int argc, char *argv[]) {
    printf("%e\n", sin(M_PI));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序提供以下输出:

1.224647e-16
Run Code Online (Sandbox Code Playgroud)

当正确的答案当然是0.

使用trig函数时,我可以期待多少舍入误差?我怎样才能最好地处理这个错误?我熟悉用于比较浮点数的最后位置单位技术,来自Bruce Dawson的比较浮点数,但这似乎在这里不起作用,因为0和1.22e-16相当于几个ULP.

c math floating-point trigonometry rounding

5
推荐指数
4
解决办法
6255
查看次数

标签 统计

c ×1

floating-point ×1

math ×1

rounding ×1

trigonometry ×1