Home Communities
IT Knowledge
Inspiration
Languages
EN

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

0 points
Created by:
297

The `Math.Acos` function returns number in radians in the range 0 to Math.PI. Based on this function we are able to calculate the inverted cosine function value.

``````using System;

public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(Math.Acos(-2));   // NaN
Console.WriteLine(Math.Acos(-1));   // 3.141592653589793 ->   180
Console.WriteLine(Math.Acos(-0.5)); // 2.0943951023931957 -> ~120
Console.WriteLine(Math.Acos(0));    // 1.5707963267948966 ->   90
Console.WriteLine(Math.Acos(0.5));  // 1.0471975511965979 ->  ~60
Console.WriteLine(Math.Acos(1));    // 0                  ->    0
Console.WriteLine(Math.Acos(2));    // NaN

Console.WriteLine(Math.Acos(0.7071067811865476)); // 0.7853981633974483 -> 45 deg
Console.WriteLine(Math.Acos(0.8660254037844387)); // 0.5235987755982987 -> 30 deg

Console.WriteLine(Math.Acos(Math.Cos(Math.PI / 4)));  // 0.7853981633974483 -> pi/4 (45deg)
Console.WriteLine(Math.Cos(Math.Acos(Math.PI / 4)));  // 0.7853981633974483 -> pi/4 (45deg)
}
}

``````

Another way to look at `Acos` function:

``````using System;

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

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

*/

double a, b, h;

// a, b and h build isosceles right triangle
a = 3;
b = a;
h = Math.Sqrt(a * a + b * b);
Console.WriteLine( CalculateAngle(b, h)); // 0.7853981633974483 <-  45 degrees

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

// a, b and h are not able to build triangle
a = 3;
b = a;
h = 1;
Console.WriteLine( CalculateAngle(b, h)); // NaN
}
}``````

1. Documentation

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

2. Working with degrees

``````using System;

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

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

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

double a, b, h;

// a, b and h build isosceles right triangle
a = 3;
b = a;
h = Math.Sqrt(a * a + b * b);
Console.WriteLine(  CalculateAngle(b, h) ); // 45 degrees

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

// a, b and h are not able to build triangle
a = 3;
b = a;
h = 1;
Console.WriteLine(  CalculateAngle(b, h) ); // NaN
}
}``````

References

1. Inverse trigonometric functions - Wikipedia

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