EN
Python - math.sin() method example
0 points
math.sin
is a method that takes only one parameter and returns an approximation of sine mathematical function.
xxxxxxxxxx
1
import math
2
3
print(math.sin(0)) # 0 <- 0 degrees
4
print(math.sin(1.5707963267948966)) # ~1 <- ~90 degrees == pi / 2
5
print(math.sin(3.1415926535897932)) # ~0 <- ~180 degrees == pi
6
print(math.sin(4.71238898038469)) # ~-1 <- ~270 degrees == -pi * (3/2)
7
print(math.sin(6.2831853071795850)) # ~0 <- ~360 degrees == pi * 2
8
9
print(math.sin(-1.5707963267948966)) # ~-1 <- ~-90 degrees == -pi / 2
Note:
1.2246467991473532e-16
and-1.133107779529596e-15
should be equal to0
but they are not because of compuptation precision error.
Syntax |
xxxxxxxxxx 1 math.sin(x) |
Parameters | x - a number that represents an angle in radians. |
Result |
If If the function can not calculate it returns |
Description | sin is a method that takes only one parameter and returns an approximation of sin(x) mathematical function. |
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.sin(rad)
11
print("sin(", rad, "rad ) =", y)
12
rad += dx
Output:
xxxxxxxxxx
1
sin(0 rad) = 0
2
sin(0.3490658503988659 rad) = 0.3420201433256687
3
sin(0.6981317007977318 rad) = 0.6427876096865393
4
sin(1.0471975511965976 rad) = 0.8660254037844386
5
sin(1.3962634015954636 rad) = 0.984807753012208
xxxxxxxxxx
1
import math
2
3
4
def calculate_sin(deg):
5
radians = (math.pi / 180) * deg
6
return math.sin(radians)
7
8
9
# Example:
10
x1 = 0.0 # beginning of calculation in degrees
11
x2 = 90.0 # ending of calculation degrees
12
13
dx = 15.0 # calculation step in degrees
14
15
deg = x1
16
while deg <= x2:
17
y = calculate_sin(deg)
18
print("sin(", deg, " deg) = ", y)
19
deg += dx
Output:
xxxxxxxxxx
1
sin(0 deg) = 0
2
sin(15 deg) = 0.25881904510252074
3
sin(30 deg) = 0.49999999999999994
4
sin(45 deg) = 0.7071067811865476
5
sin(60 deg) = 0.8660254037844386
6
sin(75 deg) = 0.9659258262890683
7
sin(90 deg) = 1
xxxxxxxxxx
1
import math
2
3
x1 = 0.0 # beginning of sine chart
4
x2 = 2 * 3.14 # end of sine 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.sin(rad) + 1
13
line = ""
14
15
y = y1
16
while y < y2:
17
line += " "
18
y += dy
19
20
print(line, "+")
21
rad += dx
Output:
xxxxxxxxxx
1
+
2
+
3
+
4
+
5
+
6
+
7
+
8
+