Why sinl give incorrect results when the argument is close to the non-zero edge of pi? Why sinl give incorrect results with a large argument? The following code illustrates this.
Note that the digits used to initialize the variable pi do not match the exact 64-bit double value. The compiler selects the closest 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; }
I am using gcc 4.7.3. Using volatile allows the compiler to replace the sinl() call with the result of hard coding. My computer has an Intel Core i7 processor and is running Windows. I print the results as double instead of long double, because the gcc mingw port I use does not support printing a long double. Here is the output of the program:
expected: -5.01656e-020, returned: -5.42101e-020 expected: -4.20533e-010, returned: -0.011874
source share