Created
October 29, 2011 19:53
-
-
Save hkulekci/1324995 to your computer and use it in GitHub Desktop.
cos(x) with approximation
This file contains hidden or 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
| #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