Python acos: What is acos() Function in Python

Python math acos() function is used to find arc cosine. The math.acos() function takes a number between -1 to 1 and finds the arc cosine of that number.

The acos() is defined under the python math library. So, we have to import the math library before using it.

Python acos

Python acos() is a built-in math function that returns the arc cosine of x in radians. The math.acos() method accepts the only number between the range of -1 to 1; if we provide a number out of the range, it returns a ValueError – “ValueError: math domain error”, and if we provide anything else except the number, it returns error TypeError – “TypeError: a float is required”.

Syntax

math.acos(number)

The acos() function takes only one argument, and the number ranges from -1 to 1.

Return Value

The acos() function returns the arc cosine of the given number.

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

Programming Example

See the following code.

# Importing math library
import math

# Taking input from user
n = int(input("Please enter number to find arc cosine: "))

# Finding cosine
print("Cosine of ", n, " is: ", math.acos(n))

# Initializing number out of range
n = 3
print("Cosine of ", n, " is: ", math.acos(n))

Output

Please enter number to find arc cosine: 1
Cosine of  1  is:  0.0
Traceback (most recent call last):
  File "acos1.py", line 12, in <module>
	print("Cosine of ",n," is: ",math.acos(n))
ValueError: math domain error

In this program, we have taken input 1 from the user and called acos() function, and we got the output cosine of 1.
On the other hand, in the next line, we have initialized the value of n as 3, which is out of range, and we got one ValueError.

Example 2

See the following code.

# Importing math library
import math

# Initializing value which is not a number
n = '100'
# Finding cosine
print("Cosine of ", n, " is: ", math.acos(n))

Output

Traceback (most recent call last):
  		File "acos2.py", line 7, in <module>
		print("Cosine of ",n," is: ",math.acos(n))
TypeError: must be real number, not str

In this program, we have declared the value of n a string, which is not a number. As said earlier, we got TypeError.

Conclusion

The Python acos Function allows you to find the trigonometry Arc Cosine for the numeric values. Python acos or Arc cosine, also called the inverse of a cosine. 

See also

Python cos

Python sin

Python tan

Leave a Comment

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