Stable Cotangent

cot(x) = cos(x)/sin(x) should be more numerically stable close to π/2 than cot(x) = 1/tan(x). You can implement that efficiently using sincos on platforms that have it. Another possibility is cot(x) = tan(M_PI_2 – x). This should be faster than the above (even if sincos is available), but it may also be less accurate, because … Read more

How to use the PI constant in C++

On some (especially older) platforms (see the comments below) you might need to and then include the necessary header file: and the value of pi can be accessed via: In my math.h (2014) it is defined as: but check your math.h for more. An extract from the “old” math.h (in 2009): However: on newer platforms (at least on my 64 bit … Read more

How to use the PI constant in C++

On some (especially older) platforms (see the comments below) you might need to and then include the necessary header file: and the value of pi can be accessed via: In my math.h (2014) it is defined as: but check your math.h for more. An extract from the “old” math.h (in 2009): However: on newer platforms (at least on my 64 bit … Read more

How to use the PI constant in C++

On some (especially older) platforms (see the comments below) you might need to and then include the necessary header file: and the value of pi can be accessed via: In my math.h (2014) it is defined as: but check your math.h for more. An extract from the “old” math.h (in 2009): However: on newer platforms … Read more

Implementing Taylor Series for sine and cosine in C

Anything over 12! is larger than can fit into a 32-bit int, so such values will overflow and therefore won’t return what you expect. Instead of computing the full factorial each time, take a look at each term in the sequence relative to the previous one. For any given term, the next one is -((x*x)/(flag_2*(flag_2-1)) times the previous one. So … Read more