Skip to content

Instantly share code, notes, and snippets.

@anishsheela
Created March 24, 2013 16:03
Show Gist options
  • Save anishsheela/5232452 to your computer and use it in GitHub Desktop.
Save anishsheela/5232452 to your computer and use it in GitHub Desktop.
sin.py - program to find the sine of an angle by iteration http://en.wikipedia.org/wiki/Sine#Series_definition
#!/usr/bin/python
"""
sin.py - program to find the sine of an angle by iteration
http://en.wikipedia.org/wiki/Sine#Series_definition
"""
# Isn't it obivious ?
from math import factorial, pi
deg_x = int(input('Enter angle in degrees:'))
# Convert to radian
x = (pi/180)*deg_x
sinx = 0.0
#Number of iterations
iterations = 20
# See http://en.wikipedia.org/wiki/Sine#Series_definition
for n in range(0, iterations):
# Calculating in 2 steps for clarity
increment = (x ** (2*n+1)) / factorial(2*n+1)
increment = increment * ((-1)**n)
sinx = sinx + increment
print "sin(", deg_x ,") = ", sinx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment