Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 6078

Python • Re: Pi Day

$
0
0
Not in Python, but one I investigated last year for calculating π on an obscure BASIC system. It didn't have it as a constant and only used degrees, so the convenient 4*ATN(1) calculation wasn't useful:

Code:

DEFDBL a-zepsilon=1e-15a=1b=1/sqr(2)t=1/4x=1DO  y=a  a=(a+b)/2  b=sqr(b*y)  t=t-x*(a-y)*(a-y)  x=2*x  IF a-b<epsilon THEN    EXIT DO  END IFLOOPPRINT (a+b)*(a+b)/(4*t)
In the end, though, I used the area of unit n-gon approximation A = ½n⋅sin(360°÷n), which with n = 36000 was more than good enough for the system's single-precision floating point. I used it later on in PostScript, too:

Code:

/pi 0.001 sin 2 div 360000 mul def
No Pi Day would be complete without a mention of Ramanujan, whose brilliantly opaque π ≅ 9801÷(2206⋅√2) is good to 7 decimal places.

In other Pi day news, the Toronto Raspberry Pi meetup reconvened after a very long hiatus

Statistics: Posted by scruss — Sun Mar 17, 2024 3:41 pm



Viewing all articles
Browse latest Browse all 6078

Trending Articles