sin, cos, tan and rounding error

前端 未结 9 615
执念已碎
执念已碎 2020-12-06 14:09

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

#include 

        
相关标签:
9条回答
  • 2020-12-06 14:25

    I rather think that will be system-dependent. I don't think the Standard has anything to say on how accurate the transcendental functions will be. Unfortunately, I don't remember seeing any discussion of function precision, so you'll probably have to figure it out yourself.

    0 讨论(0)
  • 2020-12-06 14:27

    The answer is only 0 for sin(pi) - did you include all the digits of Pi ?

    -Has anyone else noticed a distinct lack of, irony/sense of humour around here?

    0 讨论(0)
  • 2020-12-06 14:29

    I get the exact same result on my system - I'd say it is close enough

    I would solve the problem by changing the format string to "%f\n" :)

    However, this gives you a "better" result, or at least on my system it does give -3.661369e-245

    #include <stdio.h>
    #include <math.h>
    
    int main(int argc, char *argv[]) {
        printf("%e\n", (long double)sin(M_PI));
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-06 14:30

    Sine of π is 0.0.
    Sine of M_PI is about 1.224647e-16.

    M_PI is not π.

    program gives ... 1.224647e-16 when the correct answer is of course 0.

    Code gave a correct answer to 7 places.


    The following does not print the sine of π. It prints the sine of a number close to π. See below pic.

    π                            // 3.1415926535897932384626433832795...
    printf("%.21\n", M_PI);      // 3.141592653589793115998
    printf("%.21f\n", sin(M_PI));// 0.000000000000000122465
    

    Note: With the math function sine(x), the slope of the curve is -1.0 at x = π. The difference of π and M_PI is about the sin(M_PI) - as expected.


    am running into problems with rounding errors

    The rounding problem occurs when using M_PI to present π. M_PI is the double closest to π, yet since π is irrational and all finite double are rational, they must differ - even by a small amount. So not a direct rounding issue with sin(), cos(), tan(). sin(M_PI) simple exposed the issue started with using an inexact π.


    This problem, with different non-zero results of sin(M_PI), occurs if code used a different FP type like float, long double or double with something other than 53 binary bits of precision. This is not a precision issue so much as a irrational/rational one.

    0 讨论(0)
  • 2020-12-06 14:30

    Maybe too low accuracy of implementation

    M_PI = 3.14159265358979323846 (20 digits)
    

    http://fresh2refresh.com/c/c-function/c-math-h-library-functions/

    0 讨论(0)
  • 2020-12-06 14:31

    There are two sources of error. The sin() function and the approximated value of M_PI. Even if the sin() function were 'perfect', it would not return zero unless the value of M_PI were also perfect - which it is not.

    0 讨论(0)
提交回复
热议问题