Why are you comparing the term to the COSX? You should compare it to total, right?
Edit: Also you should use math functions for converting radians<->degrees https://docs.python.org/2/library/ma...lar-conversion
Edit2: This works for me. How did you come up with your term calculation?
Code:
import math
import random
print('\n%-19s%-17s%-18s%-12s%-5s\n%s' %
('Angle [deg]', 'math.cos(x)', 'Series Sum', '# of Terms', 'Equal', '-' * 71))
loop_count = 0
while loop_count <= 6:
term_count = 0
sum_cosx = 0.0
term = 1.0
angle_deg = (random.randrange(-90, 90) + random.random())
angle_rad = math.radians(angle_deg)
while abs(term) > 1e-17:
term_count += 1
sum_cosx += term
term = math.pow(-1, term_count) * math.pow(angle_rad, 2 * term_count) / \
math.factorial(2 * term_count)
math_cosx = math.cos(angle_rad)
eq = abs(math_cosx - sum_cosx) < 1e-13
print('%-19.14f%-17.14f%-18.14f%-12d%-5r' %
(angle_deg, math_cosx, sum_cosx, term_count, eq))
loop_count += 1
Code:
Angle [deg] math.cos(x) Series Sum # of Terms Equal
-----------------------------------------------------------------------
85.56252753508009 0.07737110201137 0.07737110201137 11 True
23.54675389385704 0.91673438611638 0.91673438611638 8 True
7.10812104174803 0.99231440877814 0.99231440877814 6 True
44.49776024463146 0.71327784792729 0.71327784792729 9 True
-69.96679664107417 0.34256464519610 0.34256464519610 11 True
-52.42231609149914 0.60983652931192 0.60983652931192 10 True
-76.65633541282435 0.23079132098999 0.23079132098999 11 True