annotate Demo/scripts/pi.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 33b041350034
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4573
73758db2f464 /usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
1 #! /usr/bin/env python
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # Print digits of pi forever.
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 #
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # The algorithm, using Python's 'long' integers ("bignums"), works
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # with continued fractions, and was conceived by Lambert Meertens.
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 #
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # See also the ABC Programmer's Handbook, by Geurts, Meertens & Pemberton,
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # published by Prentice-Hall (UK) Ltd., 1990.
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 import sys
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 def main():
56447
33b041350034 Remove unneeded "L" suffixes.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
14 k, a, b, a1, b1 = 2, 4, 1, 12, 4
33b041350034 Remove unneeded "L" suffixes.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
15 while True:
16704
0468596e3018 Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents: 4573
diff changeset
16 # Next approximation
56447
33b041350034 Remove unneeded "L" suffixes.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
17 p, q, k = k*k, 2*k+1, k+1
16704
0468596e3018 Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents: 4573
diff changeset
18 a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
0468596e3018 Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents: 4573
diff changeset
19 # Print common digits
49164
41524f9e2a2f #687648 from Robert Schuppenies: use classic division.
Andrew M. Kuchling <amk@amk.ca>
parents: 33456
diff changeset
20 d, d1 = a//b, a1//b1
16704
0468596e3018 Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents: 4573
diff changeset
21 while d == d1:
0468596e3018 Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents: 4573
diff changeset
22 output(d)
56447
33b041350034 Remove unneeded "L" suffixes.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
23 a, a1 = 10*(a%b), 10*(a1%b1)
49164
41524f9e2a2f #687648 from Robert Schuppenies: use classic division.
Andrew M. Kuchling <amk@amk.ca>
parents: 33456
diff changeset
24 d, d1 = a//b, a1//b1
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
1001
5067f2d38e14 all Long constants have an L suffix, not l;
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
26 def output(d):
16704
0468596e3018 Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents: 4573
diff changeset
27 # Use write() to avoid spaces between the digits
0468596e3018 Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents: 4573
diff changeset
28 sys.stdout.write(str(d))
0468596e3018 Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents: 4573
diff changeset
29 # Flush so the output is seen immediately
0468596e3018 Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents: 4573
diff changeset
30 sys.stdout.flush()
1001
5067f2d38e14 all Long constants have an L suffix, not l;
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
31
33456
487280cf71b9 Add 'if __name__ == "__main__":' to files already as a usable as a module.
Johannes Gijsbers <jlg@dds.nl>
parents: 16704
diff changeset
32 if __name__ == "__main__":
487280cf71b9 Add 'if __name__ == "__main__":' to files already as a usable as a module.
Johannes Gijsbers <jlg@dds.nl>
parents: 16704
diff changeset
33 main()