Languages
[Edit]
EN

Python - math.acos() method example

0 points
Created by:
marc_so
1014

The math.acos function returns number in radians in the range 0 to math.pi. Based on this function we are able to calculate the inverted cosine function value.

import math

print(math.acos(-2))    # NaN
print(math.acos(-1))    # 3.1415926535897930 ->  180
print(math.acos(-0.5))  # 2.0943951023931957 -> ~120
print(math.acos(0))     # 1.5707963267948966 ->   90
print(math.acos(0.5))   # 1.0471975511965979 ->  ~60
print(math.acos(1))     # 0                  ->    0
print(math.acos(2))     # NaN

print(math.acos(0.7071067811865476))  # 0.7853981633974483 -> 45 deg
print(math.acos(0.8660254037844387))  # 0.5235987755982987 -> 30 deg

print(math.acos(math.cos(math.pi / 4)))  # 0.7853981633974483 -> pi/4 (45deg)
print(math.cos(math.cos(math.pi / 4)))   # 0.7853981633974483 -> pi/4 (45deg)

 Another way to look at acos function:

import math


def calculateAngle(b, h):
    return math.acos(b / h)


#   |\
#   | \ h
# a |  \
#   |__*\ <- angle
#     b


# a, b and h build isosceles right triangle
a = 3
b = a
h = math.sqrt(a * a + b * b)
print(calculateAngle(b, h))  # 0.7853981633974483 <-  45 degrees

# a, b and h build half of equilateral triangle
a = 3
b = a * math.sqrt(3)
h = math.sqrt(a * a + b * b)
print(calculateAngle(b, h))  # 0.5235987755982987 <- ~30 degrees

# a, b and h are not able to build triangle
a = 3
b = a
h = 1
print(calculateAngle(b, h))  # ValueError

1. Documentation

Syntax
math.acos(number)
Parameters

number - a value that represents the result of the operation adjacent / hypotenuse on the right triangle.

number should be in the range -1 to +1.

Result

number value in radians in the range 0 to math.pi.

If the value can not be calculated ValueError occurs.

Description

acos is a method that takes only one parameter and returns an approximation of the result of the mathematical function arccosine(x).

2. Working with degrees

import math


def calculateAngle(b, h):
    angle = math.acos(b / h)
    return (180 / math.pi) * angle


#   |\
#   | \ h
# a |  \
#   |__*\ <- angle
#     b


# a, b and h build isosceles right triangle
a = 3
b = a
h = math.sqrt(a * a + b * b)
print(calculateAngle(b, h))  # 45 degrees

# a, b and h build half of equilateral triangle
a = 3
b = a * math.sqrt(3)
h = math.sqrt(a * a + b * b)
print(calculateAngle(b, h))  # ~30 degrees

# a, b and h are not able to build triangle
a = 3
b = a
h = 1
print(calculateAngle(b, h))  # ValueError

References

  1. Inverse trigonometric functions - Wikipedia

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Python - math module

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join