Python math.acos() Method

The math.acos() method is used to return the arc cosine of a number. The value passed in this function must be within the range of -1 to 1.

Syntax

``````import math
math.acos(x)``````

Parameters

x(required): A number for which the arc cosine is to be calculated. It accepts the only number between the range of -1 to 1.

Return Value

Returns the arc cosine of the given number in radians.

If the given number is out of range (out of -1 to 1), it returns a ValueError: math domain error. Also, if we pass anything except a number, it throws a TypeError.

Example 1: How to Use math.acos() method

``````import math

# Calculate the arc cosine of different numbers
print(math.acos(1))
print(math.acos(0))
print(math.acos(-1))
``````

Output

``````0.0
1.5707963267948966
3.141592653589793``````

You can use math.degrees() to convert radians to degrees.

Example 2: Passing non-integer number

``````import math

# Calculate the arc cosine of valid non-integer inputs
print(math.acos(0.70))
print(math.acos(-0.70)) ``````

Output

``````0.7953988301841436
2.3461938234056494``````

Example 3: Invalid number

``````import math

# Calculate the arc cosine of invalid input
print(math.acos(1.70))
``````

Output

``````Traceback (most recent call last):
File "/Users/ankitlathiya/Desktop/code/app.py", line 4, in <module>
print(math.acos(1.70))
ValueError: math domain error``````

Example 4: Plotting the math.acos()

``````import matplotlib.pyplot as plt
import numpy as np

# generate 1000 values between -1 and 1
x = np.linspace(-1, 1, 1000)

# calculate the arccos for each value
y = np.arccos(x)

# create the plot
plt.plot(x, y)

# set the title and labels
plt.title("Arccosine function")
plt.xlabel("x")
plt.ylabel("acos(x)")

# show the plot
plt.show()
``````

Output

That’s it.

This site uses Akismet to reduce spam. Learn how your comment data is processed.