Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created December 20, 2011 11:48
Show Gist options
  • Save thinkphp/1501320 to your computer and use it in GitHub Desktop.
Save thinkphp/1501320 to your computer and use it in GitHub Desktop.
math.pi
'''
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