Skip to content

Instantly share code, notes, and snippets.

@hkulekci
Created October 29, 2011 19:53
Show Gist options
  • Select an option

  • Save hkulekci/1324995 to your computer and use it in GitHub Desktop.

Select an option

Save hkulekci/1324995 to your computer and use it in GitHub Desktop.
cos(x) with approximation
#cos(x) approximation
def fak (x):
if x == 0:
return 1
else:
return x * fak(x-1)
import math
i = 2
t = math.pi / 3.0
result1 = 0.0
channel = 0.0
while i < 180:
#when i > 170, return an error like this
#OverflowError: long int too large to convert to float
if math.fmod( i / 2, 2 ) == 0:
channel = math.pow(t,i) / fak(i)
else:
channel = math.pow(t,i) / fak(i) * -1
result1 += channel
print i
i = i + 2
print "Result 1 : {0} " . format(1-result1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment