Welcome, guest | Sign In | My Account | Store | Cart

Pi is a constant, an irrational number and its notation represent the sixteenth letter of the Greek alphabet, though its value is equal to the ratio of a circle’s circumference to its diameter.

However,a Muslim scientist Abu-Abdullah Muhammad Ibn Musa al-Khwarizmi the founder of algebra ( derived from the Arabic word El-Jabr ), arithmetic, and trigonometry, an astronomer, and a geographer C850 studied and proved Pi’s irrationality.(algorithm is derived from his name Khwarizmi ).

I used the circle equation x²+y² = 1 of radius 1 centred at (0,0) and the circle area’s formula to approximate Pi value. y²= 1-x² and y = square-root(1-x²) is the upper semicircle which will extend from x = -1 to x = 1 and the rectangle method is used to approximate this area of an interval [-1,1].

The area of a circle is A = π r², thus the semicircle of radius 1 will have an area A = ½ π(1²).

Using my Pi_Approximation program allows you to represent Pi to infinity of digits, and this will depend of the project type and the precision needed, though space programs require to approximate pi at its maximum precision.

And my Pi_Approximation program allows you to make a better Pi’s approximation than the scientific Calculators

Python, 88 lines
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88``` ```#On the name of ALLAH and may the blessing and peace of Allah #be upon the Messenger of Allah Mohamed Salla Allahu Aliahi Wassalam. #Author : Fouad Teniou #Date : 23/02/10 #version :2.6 """ Pi_approximation uses the subinterval_length function and return its value and yield all the points values while using the subinterval_point function and Pi function compute Pi approximation with 16 decimal places the greater the value of the number the more precise is Pi value """ class Pi_Approximation(object): """ Class that represent Pi approximation """ def __init__(self, number): """ Pi_Approximation constructor takes the number constant """ self.number = number def subinterval_length(self): """ Compute subinterval_length """ sub_length = 2/float(self.number) return sub_length def subinterval_point(self): """ Compute the value of each point """ #attempt to yield all the Xk points values using the subinterval_point try: for item in range(1,self.number + 1): sub_point = -1 + ((item - 1/2.0)* Pi_Approximation.subinterval_length(self)) yield sub_point #Raise TypeError if input is not numerical except TypeError: print "\n Gabriel Genellina 11 years, 9 months ago

For an infinite precision generator for π, see Demo/scripts/pi.py in the Python source distribution. Stephen Chappell 11 years, 9 months ago

This is a rather simple solution for π as well:

``````def pi():
a, b, c, d, e, f = 1, 0, 1, 1, 3, 3
while True:
if a * 4 + b - c < c * e:
yield e
a, b, c, d, e, f = a * 10, (b - c * e) * 10, c, d, ((a * 3 + b) * 10) // c - e * 10, f
else:
a, b, c, d, e, f = a * d, (a * 2 + b) * f, c * f, d + 1, ((d * 7 + 2) * a + b * f) // (c * f), f + 2

digit = pi()
print(next(digit), next(digit), sep='.', end='')
while True:
print(next(digit), end='')
`````` Fouad Teniou (author) 11 years, 9 months ago

Thank you Stephen Chappell,

Not as simple as the decimal_function function from my Binary_Decimal Recursion program. Though I used the re module and the ‘.’ to make it as simple. Gabriel Genellina 11 years, 9 months ago

Note that both the demo script I posted earlier and the above code by S. Chappell only use integer arithmetic and achieve infinite precision (limited by available memory only). Your version is limited by the floating point precision, about 16 digits. Fouad Teniou (author) 11 years, 9 months ago

Thank you Gabriel Genellina,

There is no floating point limitation if you use the decimal module getcontext().prec function, or other pythons hidden functions such as repr, thus, an infinity precision, and space programs Fouad Teniou (author) 10 years, 4 months ago

A switch to C# and a new link Created by Fouad Teniou on Wed, 24 Feb 2010 (MIT)