EN
Python - math.cos() method example
0 points
The math.cos()
function returns the cosine of the specified angle in radians in the range -1
to +1
.
xxxxxxxxxx
1
import math
2
3
4
print(math.cos( 0 ) ) # 1 <- 0 degrees
5
print(math.cos( 1.5707963267948966 ) ) # ~0 <- ~90 degrees == pi / 2
6
print(math.cos( 3.1415926535897932 ) ) # ~-1 <- ~180 degrees == pi
7
print(math.cos( 4.71238898038469 ) ) # ~0 <- ~270 degrees == -pi * (3 / 2)
8
print(math.cos( 6.2831853071795850 ) ) # ~1 <- ~360 degrees == pi * 2
9
10
print(math.cos( -1.5707963267948966) ) # ~0 <- ~-90 degrees == -pi / 2
Note:
6.123233995736766e-17
,-1.8369701987210297e-16
and6.123233995736766e-17
should be equal to0
but they are not because of computation precision error.
Syntax |
xxxxxxxxxx 1 math.cos(number) |
Parameters | number - double value in radians. |
Result |
returns a numeric value between |
Description | cos is a method that takes only one parameter and returns an approximation of cos(x) mathematical function result. |
xxxxxxxxxx
1
import math
2
3
x1 = 0.0 # beginning of calculation in radians
4
x2 = math.pi / 2 # ending of calculation radians
5
6
dx = math.pi / 9 # calculation step in degrees
7
8
rad = x1
9
while rad <= x2:
10
y = math.cos(rad)
11
print("cos(", rad, " rad) = ", y)
12
rad += dx
Output:
xxxxxxxxxx
1
cos( 0.0 rad) = 1.0
2
cos( 0.3490658503988659 rad) = 0.9396926207859084
3
cos( 0.6981317007977318 rad) = 0.766044443118978
4
cos( 1.0471975511965976 rad) = 0.5000000000000001
5
cos( 1.3962634015954636 rad) = 0.17364817766693041
xxxxxxxxxx
1
import math
2
3
4
def calculate_cos(deg):
5
rad = (math.pi / 180) * deg
6
return math.cos(rad)
7
8
9
x1 = 0.0 # beginning of calculation in degrees
10
x2 = 90.0 # ending of calculation degrees
11
12
dx = 15.0 # calculation step in degrees
13
14
deg = x1
15
16
while deg <= x2:
17
y = calculate_cos(deg)
18
print("cos(", deg, " deg) = ", y)
19
deg += dx
Output:
xxxxxxxxxx
1
cos( 0.0 deg) = 1.0
2
cos( 15.0 deg) = 0.9659258262890683
3
cos( 30.0 deg) = 0.8660254037844387
4
cos( 45.0 deg) = 0.7071067811865476
5
cos( 60.0 deg) = 0.5000000000000001
6
cos( 75.0 deg) = 0.25881904510252074
7
cos( 90.0 deg) = 6.123233995736766e-17
xxxxxxxxxx
1
import math
2
3
x1 = 0.0 # beginning of cosine chart
4
x2 = 3 * 3.14 # end of cosine chart
5
6
dx = 3.14 / 4.0 # x axis step
7
dy = 1.0 / 5.0 # y axis step
8
9
rad = x1
10
while rad < x2:
11
y1 = 0.0
12
y2 = math.cos(rad) + 1
13
14
string = ""
15
16
y = y1
17
while y < y2:
18
string += " "
19
y += dy
20
21
print(string, "+")
22
rad += dx
Output:
xxxxxxxxxx
1
+
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+