Skip to content

Instantly share code, notes, and snippets.

@dheaney
Created November 18, 2015 23:21
Show Gist options
  • Save dheaney/7ba73c50dc4b4259245e to your computer and use it in GitHub Desktop.
Save dheaney/7ba73c50dc4b4259245e to your computer and use it in GitHub Desktop.
def sin(n):
# Since we're approximating with a polynomial,
# we need to artificially limit our domain to
# [-pi, pi]. That's kind of how trig works
# anyway, so it makes perfect sense.
# I just copy-pasted math.pi from the interpreter
# to save a tiny bit of load time.
if n > 3.141592653589793:
return sin(n - 2*3.141592653589793)
elif n < -3.141592653589793:
return sin(n + 2*3.141592653589793)
else:
return round(float(n -1 * n**3/6+ 1 * n**5/120+ \
-1 * n**7/5040+ 1 * n**9/362880+ \
-1 * n**11/39916800+ 1 * n**13/6227020800+ \
-1 * n**15/1307674368000+ 1 * n**17/355687428096000+ \
-1 * n**19/121645100408832000+ 1 * n**21/51090942171709440000L+ \
-1 * n**23/25852016738884976640000L+ 1 * n**25/15511210043330985984000000L+ \
-1 * n**27/10888869450418352160768000000L), 9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment