views:

89

answers:

1
+2  Q: 

Stable Cotangent

Hello,

Is there a more stable implementation for the cotangent function than return 1.0/tan(x);?

+10  A: 

cot(x) = cos(x)/sin(x) should be more numerically stable close to pi/2 than cot(x) = 1/tan(x). You can implement that efficiently using sincos on platforms that have it.

EDIT: Much better answer: cot(x) = tan(M_PI_2 - x).

Zack
Ok. I guess this is the best I can do. Also I learnt about sincos, which I must admit I had never met before!
Meh.
Zack
+1. Definitely use tan(pi/2 - x).
Alexandre C.