EN
C# / .NET - Math.Acos() method example
0 points
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.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main(string[] args)
6
{
7
// radians degrees
8
Console.WriteLine(Math.Acos(-2)); // NaN
9
Console.WriteLine(Math.Acos(-1)); // 3.141592653589793 -> 180
10
Console.WriteLine(Math.Acos(-0.5)); // 2.0943951023931957 -> ~120
11
Console.WriteLine(Math.Acos(0)); // 1.5707963267948966 -> 90
12
Console.WriteLine(Math.Acos(0.5)); // 1.0471975511965979 -> ~60
13
Console.WriteLine(Math.Acos(1)); // 0 -> 0
14
Console.WriteLine(Math.Acos(2)); // NaN
15
16
Console.WriteLine(Math.Acos(0.7071067811865476)); // 0.7853981633974483 -> 45 deg
17
Console.WriteLine(Math.Acos(0.8660254037844387)); // 0.5235987755982987 -> 30 deg
18
19
Console.WriteLine(Math.Acos(Math.Cos(Math.PI / 4))); // 0.7853981633974483 -> pi/4 (45deg)
20
Console.WriteLine(Math.Cos(Math.Acos(Math.PI / 4))); // 0.7853981633974483 -> pi/4 (45deg)
21
}
22
}
23
24
Another way to look at Acos
function:
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
static double CalculateAngle(double b, double h)
6
{
7
return Math.Acos(b / h);
8
}
9
10
public static void Main(string[] args)
11
{
12
/*
13
|\
14
| \ h
15
a | \
16
|__*\ <- angle
17
b
18
19
*/
20
21
double a, b, h;
22
23
// a, b and h build isosceles right triangle
24
a = 3;
25
b = a;
26
h = Math.Sqrt(a * a + b * b);
27
Console.WriteLine( CalculateAngle(b, h)); // 0.7853981633974483 <- 45 degrees
28
29
// a, b and h build half of equilateral triangle
30
a = 3;
31
b = a * Math.Sqrt(3);
32
h = Math.Sqrt(a * a + b * b);
33
Console.WriteLine( CalculateAngle(b, h)); // 0.5235987755982987 <- ~30 degrees
34
35
// a, b and h are not able to build triangle
36
a = 3;
37
b = a;
38
h = 1;
39
Console.WriteLine( CalculateAngle(b, h)); // NaN
40
}
41
}
Syntax |
xxxxxxxxxx 1 namespace System 2 { 3 public static class Math 4 { 5 // ... 6 public static double Acos(double number) { ... } 7 // ... 8 } 9 } |
Parameters |
|
Result |
If the value can not be calculated |
Description |
|
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
static double CalculateAngle(double b, double h)
6
{
7
double angle = Math.Acos(b / h);
8
9
return (180 / Math.PI) * angle; // rad to deg conversion
10
}
11
12
public static void Main(string[] args)
13
{
14
/*
15
|\
16
| \ h
17
a | \
18
|__*\ <- angle
19
b
20
*/
21
22
double a, b, h;
23
24
// a, b and h build isosceles right triangle
25
a = 3;
26
b = a;
27
h = Math.Sqrt(a * a + b * b);
28
Console.WriteLine( CalculateAngle(b, h) ); // 45 degrees
29
30
// a, b and h build half of equilateral triangle
31
a = 3;
32
b = a * Math.Sqrt(3);
33
h = Math.Sqrt(a * a + b * b);
34
Console.WriteLine( CalculateAngle(b, h) ); // ~30 degrees
35
36
// a, b and h are not able to build triangle
37
a = 3;
38
b = a;
39
h = 1;
40
Console.WriteLine( CalculateAngle(b, h) ); // NaN
41
}
42
}