罪过,cos,棕褐色不准确 [英] sin, cos, tan not accurate

查看:74
本文介绍了罪过,cos,棕褐色不准确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当参数接近pi的非零倍时,为什么sinl给出不正确的结果?当参数很大时,为什么sinl给出不正确的结果?以下代码说明了这一点.

Why does sinl give incorrect results when the argument is near a non-zero multiple of pi? Why does sinl give incorrect results when the argument is large? The following code illustrates that.

请注意,用于初始化变量pi的数字与任何64位长双精度值都不完全匹配.编译器选择最接近的值3.14159265358979323851280895940618620443274267017841339111328125.可以使用libquadmath,gnu MPFR lib或在线计算器(例如 http://www)找到期望的正弦值. ttmath.org/online_calculator .

Note that the digits used to initialize variable pi do not exactly match any 64-bit long double value. The compiler chooses the nearest value, which is 3.14159265358979323851280895940618620443274267017841339111328125. The expected sine value can be found using libquadmath, gnu MPFR lib, or an online calculator such as http://www.ttmath.org/online_calculator.

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

int main (int argc, char *argv [])
    {
    volatile long double pi = 3.14159265358979323846L;
    volatile long double big = 9223372035086174241L;
    volatile long double expected1 = -5.0165576126683320235E-20L;
    volatile long double expected2 = -4.2053336735954077951E-10L;
    double result;
    double ex1 = expected1, ex2 = expected2;

    result = sinl (pi);
    printf("expected: %g, \nreturned: %g\n\n", ex1, result);
    result = sinl (big);
    printf("expected: %g, \nreturned: %g\n\n", ex2, result);
    return 0;
    }

我正在使用gcc 4.7.3.使用volatile使编译器无法用硬编码结果替换sinl()调用.我的计算机装有Intel Core i7处理器并运行Windows.我将结果打印为double而不是long double,因为我使用的gcc的mingw端口不支持打印long double.这是程序输出:

I am using gcc 4.7.3. The use of volatile keeps the compiler from replacing the sinl() call with a hard-coding result. My computer has an Intel Core i7 processor and runs Windows. I am printing the results as double instead of long double because the mingw port of gcc I use does not support printing long double. Here is the program output:

expected: -5.01656e-020,
returned: -5.42101e-020

expected: -4.20533e-010,
returned: -0.011874

推荐答案

可以将不准确性追溯到sinl库代码使用的fsin处理器指令.英特尔声称fsin,fcos和fptan指令的精度不能达到1.0 ulp: http://notabs.org/fpuaccuracy/

The inaccuracy can be traced to the fsin processor instruction used by the sinl library code. The instructions fsin, fcos, and fptan are not accurate to 1.0 ulp as Intel claims: http://notabs.org/fpuaccuracy/

这篇关于罪过,cos,棕褐色不准确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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