Home Communities
IT Knowledge
Inspiration
Languages
EN

C# / .NET - Math.Atan() method example

0 points
Created by:
528

The `Math.Atan` function returns number in radians in the range `-Math.PI/2` to `+Math.PI/2`. The function calculates the inverted tangent function value.

``````using System;

public class Program
{
static double CalculateAngle(double a, double b)
{
return Math.Atan(a / b);
}

public static void Main(string[] args)
{
/*
|\
| \ h
a |  \
|__*\ <- angle
b
*/

double a, b;

// a an b build isosceles right triangle
a = 3;
b = a;
Console.WriteLine(  CalculateAngle(a, b)); // 0.7853981633974483 <- ~45 degrees

// a and b build half of equilateral triangle
a = 3;
b = a * Math.Sqrt(3);
Console.WriteLine(  CalculateAngle(a, b)); // 0.5235987755982988 <- ~30 degrees

// a and b build very high (+Inf) and slim (~0) triangle
a = Double.PositiveInfinity;
b = 0;
Console.WriteLine(  CalculateAngle(a, b)); // 1.5707963267948966 <- ~90 degrees
}
}``````

1. Documentation

 Syntax ``````namespace System { public static class Math { // ... public static double Atan(double number) { ... } // ... } }`````` Parameters `number` - double number value that represents the result of the operation `opposite / adjacent` on the right triangle (primitive value). Result `number` value in radians in the range `-Math.PI/2` to `+Math.PI/2` (primitive value). If value can not be calculated `NaN` is returned. Description `Atan` is a static method that takes only one parameter and returns an approximation of the result of the mathematical function arctangent(x).

2. Working with degrees

``````using System;

public class Program
{
static double CalculateAngle(double a, double b)
{
double angle = Math.Atan(a / b);

return (180 / Math.PI) * angle; // rad to deg conversion
}

public static void Main(string[] args)
{
/*
|\
| \ h
a |  \
|__*\ <- angle
b
*/

double a, b;

// a an b build isosceles right triangle
a = 3;
b = a;
Console.WriteLine(CalculateAngle(a, b)); // ~45 degrees

// a and b build half of equilateral triangle
a = 3;
b = a * Math.Sqrt(3);
Console.WriteLine(CalculateAngle(a, b)); // ~30 degrees

// a and b build very high (+Inf) and slim (~0) triangle
a = Double.PositiveInfinity;
b = 0;
Console.WriteLine(CalculateAngle(a, b)); // ~90 degrees
}
}``````

References

1. Inverse trigonometric functions - Wikipedia

Join to our subscribers to be up to date with content, news and offers.