| by Arround The Web | No comments

Acos Python

Python is a simple yet advanced programming language that offers a variety of functions and modules to work with mathematical computation. The standard “math” library provides many built-in functions for solving any math problem. Some of these functions include “log()”, “degree()”, “tan()”, “acos()” etc. This article provides an in-depth guide on Python’s “acos()” function.

What is “math.acos()” Function in Python?

The Python “acos()” function of the “math” module provides the “arc” cosine value of a particular number. The number passed to the function must be between “-1” and “1”, and the returned value will be in radians, between “0” and “pi”.

Syntax

math.acos(x)

 

Parameters

In the above syntax, the “x” parameter is the number whose “arc” cosine is to be calculated.

Returns Value

This function retrieves a float value that corresponds to the “arc cosine” of “x”.

Example 1: Applying the “math.acos()” Function to Compute the Arc Cosine Value

The below code is used to return the arc cosine value of the specified numbers:

import math
print("acos(0.75) : ",  math.acos(0.75), "Radians")
print("acos(-0.75) : ",  math.acos(-0.75), "Radians")

 

In the above code, the “math.acos()” function takes the positive and negative numbers between the discussed range and returns the corresponding arc cosine value.

Output

The inverse cosine of the given numbers has been displayed in the above output.

Example 2: Applying the “math.acos()” and the “math.degrees()” Functions to Convert the Inverse Cosine Radian Value Into Degree

The following code utilizes the combined “math.acos()” and the “math.degrees()” functions to convert the resultant radian values into degrees:

import math
val_1 = math.acos(0.75)
val_2 = math.acos(-0.75)
print("acos(0.75) : ", math.degrees(val_1) , "Degrees")
print("acos(-0.75) : ",  math.degrees(val_2), "Degrees")

 

In the above code, the “math.acos()” function is used to retrieve the inverse of the cosine. The “math.degrees()” function then takes the inverse cosine value as an argument and converts this radian value into degrees.

Output

The specified arc cosine value of the given numbers has been represented in degrees accordingly.

Example 3: Facing an Exception With the “math.acos()” Function

In this example, the “math domain” error can be encountered upon specifying a number (to be evaluated for inverse of the cosine) outside the number limits:

import math
print("acos(2) : ",  math.acos(2), "Radians")
print("acos(-2) : ",  math.acos(-2), "Radians")

 

In the above code, the “math.acos()” function takes the number “2” and “-2” to compute their inverse cosine which is not allowed in the specified range “[-1, 1]”.

Output

This output implies that the given number is not in the allowed range.

Note: In order to fix the above “ValueError”, you need to use the numbers between “-1” and “1”.

Conclusion

The “math.acos()” function of the “math” library is used to determine the inverse cosine of the specified numbers in radians. The “math.degrees()” function can also be used along with the “math.acos()” function to convert the inverse cosine radians value into degrees. Also, this function returns a “ValueError” if the provided value/number is not in the range “[-1, 1]”. This Python guide presented a thorough guide on the “math.acos()” function using multiple examples.

Share Button

Source: linuxhint.com

Leave a Reply