EN
Python - math.acos() method example
0 points
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.
xxxxxxxxxx
1
import math
2
3
print(math.acos(-2)) # NaN
4
print(math.acos(-1)) # 3.1415926535897930 -> 180
5
print(math.acos(-0.5)) # 2.0943951023931957 -> ~120
6
print(math.acos(0)) # 1.5707963267948966 -> 90
7
print(math.acos(0.5)) # 1.0471975511965979 -> ~60
8
print(math.acos(1)) # 0 -> 0
9
print(math.acos(2)) # NaN
10
11
print(math.acos(0.7071067811865476)) # 0.7853981633974483 -> 45 deg
12
print(math.acos(0.8660254037844387)) # 0.5235987755982987 -> 30 deg
13
14
print(math.acos(math.cos(math.pi / 4))) # 0.7853981633974483 -> pi/4 (45deg)
15
print(math.cos(math.cos(math.pi / 4))) # 0.7853981633974483 -> pi/4 (45deg)
Another way to look at acos
function:
xxxxxxxxxx
1
import math
2
3
4
def calculateAngle(b, h):
5
return math.acos(b / h)
6
7
8
# |\
9
# | \ h
10
# a | \
11
# |__*\ <- angle
12
# b
13
14
15
# a, b and h build isosceles right triangle
16
a = 3
17
b = a
18
h = math.sqrt(a * a + b * b)
19
print(calculateAngle(b, h)) # 0.7853981633974483 <- 45 degrees
20
21
# a, b and h build half of equilateral triangle
22
a = 3
23
b = a * math.sqrt(3)
24
h = math.sqrt(a * a + b * b)
25
print(calculateAngle(b, h)) # 0.5235987755982987 <- ~30 degrees
26
27
# a, b and h are not able to build triangle
28
a = 3
29
b = a
30
h = 1
31
print(calculateAngle(b, h)) # ValueError
Syntax |
xxxxxxxxxx 1 math.acos(number) |
Parameters |
|
Result |
If the value can not be calculated |
Description |
|
xxxxxxxxxx
1
import math
2
3
4
def calculateAngle(b, h):
5
angle = math.acos(b / h)
6
return (180 / math.pi) * angle
7
8
9
# |\
10
# | \ h
11
# a | \
12
# |__*\ <- angle
13
# b
14
15
16
# a, b and h build isosceles right triangle
17
a = 3
18
b = a
19
h = math.sqrt(a * a + b * b)
20
print(calculateAngle(b, h)) # 45 degrees
21
22
# a, b and h build half of equilateral triangle
23
a = 3
24
b = a * math.sqrt(3)
25
h = math.sqrt(a * a + b * b)
26
print(calculateAngle(b, h)) # ~30 degrees
27
28
# a, b and h are not able to build triangle
29
a = 3
30
b = a
31
h = 1
32
print(calculateAngle(b, h)) # ValueError
33