Introduction Python math.acos() Method with Examples

math.acos() method is a library method of math module, it is used to get the arc cosine, it accepts a number between -1 to 1 and returns the arc cosine value (in radians) of the given number.

Note: 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”.

Python math.acos()

Syntax


math.acos(number);


Parameter(s):

  • number – is the number whose arc cosine to be calculated.

Return value:

  • float – it returns a float value that is the arc cosine value of the number

Examples of Python math.acos() method

Example 1


# python code to demonstrate example of 
# math.acos() method 

# importing math module
import math

# number
a = -1
print("acos(",a,") is = ", math.acos(a))

a = 0
print("acos(",a,") is = ", math.acos(a))

a = 0.278
print("acos(",a,") is = ", math.acos(a))

a = 1
print("acos(",a,") is = ", math.acos(a))



Output


acos( -1 ) is =  3.141592653589793
acos( 0 ) is =  1.5707963267948966
acos( 0.278 ) is =  1.2890849198520296
acos( 1 ) is =  0.0

Example 2

ValueError example



# 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


Example 3

TypeError example



# 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.

Thanks for reading !

#python #programming #Python math.acos

Introduction Python math.acos() Method with Examples
12.55 GEEK