Created
December 20, 2011 11:48
-
-
Save thinkphp/1501320 to your computer and use it in GitHub Desktop.
math.pi
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
''' | |
Calculate the value of PI using Infinite Series | |
Twitter: @thinkphp | |
Website: http://thinkphp.ro | |
G+ : http://gplus.to/thinkphp | |
MIT Style License | |
4*(1- 1/3 + 1/5 - 1/7 + 1/9 +...) | |
''' | |
import math | |
def PI(): | |
v1 = 1 | |
v2 = v1 - float(1.0 / 3.0) | |
sign = 1 | |
i = 5 | |
EPS = 0.00001 | |
while 4*abs(v1-v2) >= EPS: | |
v1 = v2 | |
v2 = v2 + sign*float(1.0/i) | |
sign = sign*(-1) | |
i += 2 | |
return 4*v2 | |
print 'def PI: ', PI() | |
print 'math.pi: ', math.pi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment