Created
March 24, 2013 16:03
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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