EN
Python - math.tan() method example
0 points
math.tan()
is a method that takes only one parameter and returns the approximated value of the tangent mathematical function.
xxxxxxxxxx
1
import math
2
3
print( math.tan( 0 ) ) # 0 <- 0 degrees
4
5
print( math.tan( 0.7853981633974483 ) ) # ~1 <- ~45 degrees == pi / 4
6
print( math.tan( 1.5707963267948966 ) ) #~+inf <- ~90 degrees == pi / 2
7
8
print( math.tan(-0.7853981633974483 ) ) # ~-1 <- ~-45 degrees == -pi / 4
9
print( math.tan(-1.5707963267948966 ) ) #~-inf <- ~-90 degrees == -pi / 2
Note:
0.9999999999999999
,16331239353195370
,-0.9999999999999999
and-16331239353195370
should be equal to1
,+Inf
,-1
and-Inf
but they are not because of compuptation precision error.
Syntax |
xxxxxxxxxx 1 math.tan(x) |
Parameters | x - value in radians. |
Result | value calculated as tan(x) mathematical function. |
Description | math.tan() is a method that takes only one argument and returns the approximated value of the tangent 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.tan(rad)
11
print("tan(", rad, " rad ) = ", y)
12
rad += dx
Output:
xxxxxxxxxx
1
tan( 0.0 rad ) = 0.0
2
tan( 0.3490658503988659 rad ) = 0.36397023426620234
3
tan( 0.6981317007977318 rad ) = 0.8390996311772799
4
tan( 1.0471975511965976 rad ) = 1.7320508075688767
5
tan( 1.3962634015954636 rad ) = 5.671281819617707
xxxxxxxxxx
1
import math
2
3
4
def calculate_tan(deg):
5
radians = (math.pi / 180) * deg
6
7
return math.tan(radians)
8
9
10
# Example:
11
x1 = 0.0 # beginning of calculation in degrees
12
x2 = 90.0 # ending of calculation degrees
13
14
dx = 30.0 # calculation step in degrees
15
16
deg = x1
17
while deg <= x2:
18
y = calculate_tan(deg)
19
print("tan(", deg, " deg ) = ", y)
20
deg += dx
Output:
xxxxxxxxxx
1
tan(0 deg) = 0
2
tan(30 deg) = 0.5773502691896257
3
tan(60 deg) = 1.7320508075688767
4
tan(90 deg) = 16331239353195370
xxxxxxxxxx
1
import math
2
3
4
def print_line(y1, y2, dy, character):
5
line = ""
6
7
y = y1
8
while y < y2:
9
line += " "
10
y += dy
11
12
print(line, character)
13
14
15
x1 = -3.14 # begining of sine chart
16
x2 = 3.14 # end of sine chart
17
18
y1 = -4.0
19
y2 = 4.0
20
21
x_steps = 60
22
y_steps = 60
23
24
dx = (x2 - x1) / x_steps # x axis step
25
dy = (y2 - y1) / y_steps # y axis step
26
27
rad = x1
28
while rad < x2:
29
y = math.tan(rad)
30
31
if y <= y1 or y >= y2:
32
print(" ")
33
else:
34
print_line(y1, y, dy, 'x')
35
36
rad += dx
Output:
xxxxxxxxxx
1
x
2
x
3
x
4
x
5
x
6
x
7
x
8
x
9
x
10
x
11
x
12
x
13
x
14
15
16
17
18
19
x
20
x
21
x
22
x
23
x
24
x
25
x
26
x
27
x
28
x
29
x
30
x
31
x
32
x
33
x
34
x
35
x
36
x
37
x
38
x
39
x
40
x
41
x
42
x
43
x
44
45
46
47
48
49
x
50
x
51
x
52
x
53
x
54
x
55
x
56
x
57
x
58
x
59
x
60
x