Is there a more stable implementation for the cotangent function than return 1.0/tan(x);?
Is there a more stable implementation for the cotangent function than return 1.0/tan(x);?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
cot(x) = cos(x)/sin(x)should be more numerically stable close to π/2 thancot(x) = 1/tan(x). You can implement that efficiently usingsincoson platforms that have it.Another possibility is
cot(x) = tan(M_PI_2 - x). This should be faster than the above (even ifsincosis available), but it may also be less accurate, becauseM_PI_2is of course only an approximation of the transcendental number π/2, so the differenceM_PI_2 - xwill not be accurate to the full width of adoublemantissa — in fact, if you get unlucky, it may have only a few meaningful bits.