| Deutsch English Français Italiano |
|
<vrj08c$ugqc$2@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Lawrence D'Oliveiro <ldo@nz.invalid>
Newsgroups: comp.misc,sci.electronics.design
Subject: Re: Totally OT: Colliding blocks that compute pi
Date: Fri, 21 Mar 2025 06:16:44 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <vrj08c$ugqc$2@dont-email.me>
References: <m3snqdFss95U1@mid.individual.net>
<87msdihv8l.fsf@tilde.institute> <RpUohCCnw$2nFwmw@b-howie.co.uk>
<vrh4dr$38mq5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 21 Mar 2025 07:16:44 +0100 (CET)
Injection-Info: dont-email.me; posting-host="992507decd468f9ef8cbeb6e70190836";
logging-data="1000268"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+WT3X2uR/mk0yGBJV+cQv6"
User-Agent: Pan/0.162 (Pokrosvk)
Cancel-Lock: sha1:vNP3BuMhZHwMv4VmRDVYb0odnTU=
Bytes: 2306
On Thu, 20 Mar 2025 13:15:38 +0000, SH wrote:
> How I wish I could calculate pi
Your computer can do it!
import decimal
Dec = decimal.Decimal
decctx = decimal.getcontext()
decctx.prec = 64
def decimal_pi():
with decimal.localcontext() as decctx :
decctx.prec += 2 # extra digits for intermediate steps
t = Dec(3) # substitute 3.0 for regular floats
lasts, s, n, na, d, da = 0, 3, 1, 0, 0, 24
nr_steps = 0
while s != lasts :
nr_steps += 1
lasts = s
n, na = n + na, na + 8
d, da = d + da, da + 32
t = t * n / d
s += t
#end while
#end with
print("nr_steps = %d" % nr_steps)
return +s # unary plus applies the new precision
#end decimal_pi
print(decimal_pi())
output:
nr_steps = 104
3.141592653589793238462643383279502884197169399375105820974944592
Taken from a presentation I did here
<https://github.com/HamPUG/meetings/blob/master/2022/2022-11-14/ldo/Continued%20Fractions.ipynb>.
I tried continued fractions, but found them a waste of time.