Sin and Cos give unexpected results for well-known angles

C/C++ provides sin(a), cos(a), tan(a), etc. functions that require a parameter with radian units rather than degrees. double DegreesToRadians(d) performs a conversion that is close but an approximate as the conversion results are rounded. Also machine M_PI is close, but not the same value as the the mathematical irrational π. OP’s code with 180 passed … Read more

How to use the PI constant in C++

On some (especially older) platforms (see the comments below) you might need to #define _USE_MATH_DEFINES and then include the necessary header file: #include <math.h> and the value of pi can be accessed via: M_PI In my math.h (2014) it is defined as: # define M_PI 3.14159265358979323846 /* pi */ but check your math.h for more. … Read more

How does C compute sin() and other math functions?

In GNU libm, the implementation of sin is system-dependent. Therefore you can find the implementation, for each platform, somewhere in the appropriate subdirectory of sysdeps. One directory includes an implementation in C, contributed by IBM. Since October 2011, this is the code that actually runs when you call sin() on a typical x86-64 Linux system. … Read more