Python 3 – Number acos() Method
The acos() method in Python 3 is used to return the arc cosine of x in radians. The input for this function should be a number between -1 and 1. If the input is outside this range, the function will raise the ValueError.
Syntax:
import math
math.acos(x)
Parameters:
- x: Required argument which represents the value whose arc cosine needs to be calculated. This value should be a numeric value between -1 and 1.
Return Value:
acos() method returns the arc cosine of x in radians.
Let’s take a look at some examples to understand how this method works:
Example 1:
import math
print(math.acos(0.5))
Output:
1.0471975511965979
In the above example, we have imported the math module and used the acos() method to calculate the arc cosine of 0.5. The output of the program is 1.0471975511965979.
Example 2:
import math
print(math.acos(-1))
Output:
3.141592653589793
In the above example, we have used the acos() method to calculate the arc cosine of -1. The output of the program is 3.141592653589793.
Example 3:
import math
print(math.acos(1.1))
Output:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: math domain error
In the above example, we have used the acos() method to calculate the arc cosine of 1.1. Since the input value is outside the range of -1 to 1, the function has raised a ValueError.
Example 4:
import math
x = 0.5
acos_x = math.acos(x)
print(f"The arc cosine of {x} is {acos_x} radians.")
Output:
The arc cosine of 0.5 is 1.0471975511965979 radians.
In the above example, we have used the format string to print the input value and its corresponding arc cosine value.
Conclusion
In conclusion, the acos() method in Python 3 can be used to calculate the arc cosine of a number between -1 and 1 in radians. It is an important function for trigonometric calculations and is widely used in various mathematical applications. It is important to check the input value before using the function to avoid any exceptions in the program.