AppDividend
Latest Code Tutorials

Python acos: How to Use Math.acos() Function Example

0

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 function is under the python math library, and we have to import the math library before using it.

Python acos()

Python math acos() method 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 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 ranged 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), then it returns a ValueError. Also, if we pass anything except number, then 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 is 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 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 log(10)

Python log(2)

Python log(x, base)

Python log1p(x)

Python exp()

Leave A Reply

Your email address will not be published.

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