Arc tangent function in C#

Asked

Viewed 644 times

6

I did the following calculation on the calculator:

arctg(50/10) = 78,69°

However, when doing in the code, using the function Math.Atan, the result is as follows:

Codigo

Is there any other way to calculate the Arc Tangent ?

  • 1

    Raphael, take a look at the documentation https://msdn.microsoft.com/pt-br/library/system.math.atan(v=vs.110). aspx there shows that the function returns the Angle of the tangent and not the arc, I do not remember well about trigonometry, but I believe they are distinct things. And if you see the example, it has how to calculate the tangent arc.

  • @Pablovargas, they’re the same thing, basically. The angle returned is the one referring to the arc in question. The same problem was the disparity of the units, as the bigown replied. Just convert from radian to degrees.

2 answers

12


To documentation says that the method input should be in radians and you are using degrees. It has to convert radian to degree before. The calculator is already in degrees, so it worked.

using static System.Console;
using static System.Math;
                    
public class Program {
    public static void Main() => WriteLine(Atan(5) * 180 / PI);
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Maybe you want to create functions to make the conversion:

public static class MathUtil {
    public static double DegreeToRadian(double angle) => PI * angle / 180.0;
    public static double RadianToDegree(double angle) => angle * (180.0 / PI);
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Related.

5

Browser other questions tagged

You are not signed in. Login or sign up in order to post.