We know that the inverse of tan
is Math.Atan
, what about the inverse of cot? cot(x)
is defined as
cot(x)=1/tan(x)
See Inverse Cotangent. For some values, the inverse would be atan(1/x)
.
There are two commonly used conventions for the inverse of Cotangent, since e.g. cot(1) = cot(1-pi). It is important to think through which definition one wishes to use.
Let arccot(x) match atan(1/x), with values between -pi/2 and pi/2. This gives a discontinuity at 0 (jumps from -pi/2 to pi/2). This is the convention used in Greg Hew's answer.
public static double Acot(double x)
{
return (x < 0 ? -Math.PI/2 : Math.PI/2) - Math.Atan(x);
}
or
public static double Acot(double x)
{
return x == 0 ? 0 : Math.Atan(1/x);
}
Let arccot(x) be a continuous function with values between 0 and pi. This is the convention used in Daniel Martin's answer.
public static double Acot(double x)
{
return Math.PI/2 - Math.Atan(x);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With