C# Math.Acos() – Examples
In this tutorial, we will learn about the C# Math.Acos() method, and learn how to use this method to find the arc cosine of given double value, with the help of examples.
Acos(Double)
Math.Acos(d) returns the angle whose cosine is the specified number d
. The angle returned shall be in radians.
If a value greater than 1 or less than -1 is given as argument, Acos() returns Double.NaN
.
Syntax
The syntax of Acos(d) method is
Math.Acos(Double d)
where
Parameter | Description |
---|---|
d | The double value for which we have to find arc cosine. |
Return Value
The method returns value of type double.
Example 1 – Math.Acos(0.5)
In this example, we will find the angle for which cosine value is 0.5. We shall print the result both in radians and in degrees.
We know that cos(60 degrees) = 0.5.
C# Program
using System;
class Example {
static void Main(string[] args) {
Double d = 0.5;
Double angle = Math.Acos(d);
Console.WriteLine($"Angle in radians : {angle}");
Console.WriteLine($"Angle in degrees : {(180 / Math.PI) * angle}");
}
}
Output
Angle in radians : 1.0471975511966
Angle in degrees : 60
Example 2 – Math.Acos(1.0)
In this example, we will find the angle for which cosine value is 1.0. We know that cos(0 degrees) = 1.0.
C# Program
using System;
class Example {
static void Main(string[] args) {
Double d = 1.0;
Double angle = Math.Acos(d);
Console.WriteLine($"Angle in radians : {angle}");
Console.WriteLine($"Angle in degrees : {(180 / Math.PI) * angle}");
}
}
Output
Angle in radians : 0
Angle in degrees : 0
Example 3 – Acos(0)
In this example, we will find the angle for which cosine value is 0. We know that cos(90 degrees) = 0.
C# Program
using System;
class Example {
static void Main(string[] args) {
Double d = 0;
Double angle = Math.Acos(d);
Console.WriteLine($"Angle in radians : {angle}");
Console.WriteLine($"Angle in degrees : {(180 / Math.PI) * angle}");
}
}
Output
Angle in radians : 1.5707963267949
Angle in degrees : 90
Example 4 – Acos(5) – Invalid Argument
In this example, we will find the angle for which cosine value is 5. We know that the range of cos(angle) is [-1.0, 1.0], and since the given argument is out of range, Acos() returns Double.NaN.
C# Program
using System;
class Example {
static void Main(string[] args) {
Double d = 5;
Double angle = Math.Acos(d);
Console.WriteLine($"Angle in radians : {angle}");
Console.WriteLine($"Angle in degrees : {(180 / Math.PI) * angle}");
}
}
Output
Angle in radians : NaN
Angle in degrees : NaN
Conclusion
In this C# Tutorial, we have learnt the syntax of C# Math.Acos() method, and also learnt how to use this method with the help of C# example programs.