Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inverse cot in C#

Tags:

c#

math

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)
like image 450
Graviton Avatar asked May 06 '10 07:05

Graviton


2 Answers

See Inverse Cotangent. For some values, the inverse would be atan(1/x).

like image 147
Greg Hewgill Avatar answered Sep 20 '22 21:09

Greg Hewgill


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.

  1. 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.

    arccot with values between -pi/2 and pi/2, image from Wolfram MathWorld

    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);
    }
    
  2. Let arccot(x) be a continuous function with values between 0 and pi. This is the convention used in Daniel Martin's answer.

    arccot with values between 0 and pi, image from Wikipedia

    public static double Acot(double x)
    {
        return Math.PI/2 - Math.Atan(x);
    }
    
like image 23
johv Avatar answered Sep 18 '22 21:09

johv