Adventures in Python:
Trigonometric Tables
One of the many mathematical modules available for Python is
the math module. The math functions work
with real numbers.
The program highlighted today also features the for
loop. The for loop is structured differently
from what I’m used to. The for loop
works with a range or a list instead of a count of values.
The range function creates a list of integers, which varies
with the syntax:
range(n): creates a
list of integers from 0 to n-1 (the n-1 will take getting used to)
range(a,b): creates a
list of integers from a to b-1
The default angle measurement in Python is radians. The program builds a tables in increments of
10°, so I must use the math.radians function to convert angles to radians for
the table to work properly.
# Program 003: Trig tables
# Python works with
radians
# If you want
degrees, use the math.radians, math.degrees to convert
# Import the math
library
import math
# Header
print("Angle","sin","cos","tan")
# Build a table with
0 to 180 degrees of trig (sin, cos, tan)
# range: range(start, stop, step)
# range ends when
stop is equalled or exceeded
# since I want 180,
I will tip the upper limit to 181
# The for structure
defaults to integers
# For structure is
used
# rounded values
print("Rounded
to 6 decimal places")
print("A list
format is used")
print(["angle","
sin "," cos "," tan "])
for k in
range(0,181,10):
# round each compoment
s = round(math.sin(math.radians(k)),6)
c = round(math.cos(math.radians(k)),6)
t = round(math.tan(math.radians(k)),6)
print([k,s,c,t])
Output:
Angle sin
cos tan
Rounded
to 6 decimal places
A list
format is used
['angle',
' sin ', ' cos ', ' tan ']
[0, 0.0,
1.0, 0.0]
[10,
0.173648, 0.984808, 0.176327]
[20,
0.34202, 0.939693, 0.36397]
[30, 0.5,
0.866025, 0.57735]
[40,
0.642788, 0.766044, 0.8391]
[50,
0.766044, 0.642788, 1.191754]
[60,
0.866025, 0.5, 1.732051]
[70,
0.939693, 0.34202, 2.747477]
[80,
0.984808, 0.173648, 5.671282]
[90, 1.0,
0.0, 1.633123935319537e+16]
[100,
0.984808, -0.173648, -5.671282]
[110,
0.939693, -0.34202, -2.747477]
[120,
0.866025, -0.5, -1.732051]
[130,
0.766044, -0.642788, -1.191754]
[140,
0.642788, -0.766044, -0.8391]
[150,
0.5, -0.866025, -0.57735]
[160,
0.34202, -0.939693, -0.36397]
[170,
0.173648, -0.984808, -0.176327]
[180,
0.0, -1.0, -0.0]
Eddie
This blog is property of Edward Shore, 2017.