Files
Sergey B Kirpichev 0e1bd9496d Restrict libmp exports to public API
This is following namespace:
```
{'BACKEND', 'ComplexResult', 'MPZ', 'MPZ_ONE', 'NoConvergence',
 'catalan_fixed', 'dps_to_prec', 'euler_fixed', 'fhalf', 'finf',
 'fnan', 'fninf', 'fnone', 'fone', 'from_float', 'from_int', 'from_man_exp',
 'from_rational', 'from_str', 'fzero', 'giant_steps', 'ifac', 'ifib',
 'int_types', 'isqrt', 'mpc_abs', 'mpc_exp', 'mpc_pow', 'mpc_pow_int',
 'mpc_pow_mpf', 'mpc_sqrt', 'mpf_abs', 'mpf_add', 'mpf_atan', 'mpf_atan2',
 'mpf_bernoulli', 'mpf_ceil', 'mpf_cmp', 'mpf_cos', 'mpf_cosh_sinh', 'mpf_div',
 'mpf_e', 'mpf_eq', 'mpf_exp', 'mpf_floor', 'mpf_ge', 'mpf_gt', 'mpf_le',
 'mpf_log', 'mpf_lt', 'mpf_mod', 'mpf_mul', 'mpf_neg', 'mpf_pi', 'mpf_pow',
 'mpf_pow_int', 'mpf_shift', 'mpf_sign', 'mpf_sin', 'mpf_sqrt', 'mpf_sub',
 'mpf_tan', 'normalize', 'phi_fixed', 'prec_to_dps', 'repr_dps',
 'round_nearest', 'sqrtrem', 'to_float', 'to_int', 'to_man_exp', 'to_rational',
 'to_str'}
```

Closes #704
2026-05-25 08:31:19 +03:00

83 lines
2.4 KiB
Python

"""
Calculate digits of pi. This module can be run interactively with
python pidigits.py
"""
import math
import sys
from time import perf_counter
from mpmath.libmp.libelefun import pi_fixed
from mpmath.libmp.libintmath import bin_to_radix, numeral
def display_fraction(digits, skip=0, colwidth=10, columns=5):
perline = colwidth * columns
printed = 0
for linecount in range((len(digits)-skip) // (colwidth * columns)):
line = digits[skip+linecount*perline:skip+(linecount+1)*perline]
for i in range(columns):
print(line[i*colwidth : (i+1)*colwidth], end=' ')
print(":", (linecount+1)*perline)
if (linecount+1) % 10 == 0:
print()
printed += colwidth*columns
rem = (len(digits)-skip) % (colwidth * columns)
if rem:
buf = digits[-rem:]
s = ""
for i in range(columns):
s += buf[:colwidth].ljust(colwidth+1, " ")
buf = buf[colwidth:]
print(s + ":", printed + colwidth*columns)
def calculateit(base, n, tofile):
intpart = numeral(3, base)
skip = 1
if base <= 3:
skip = 2
prec = int(n*math.log(base,2))+10
print("Step 1 of 2: calculating binary value...")
t = perf_counter()
a = pi_fixed(prec, verbose=True, verbose_base=base)
step1_time = perf_counter() - t
print("Step 2 of 2: converting to specified base...")
t = perf_counter()
d = bin_to_radix(a, prec, base, n)
d = numeral(d, base, n)
step2_time = perf_counter() - t
print("\nWriting output...\n")
if tofile:
out_ = sys.stdout
sys.stdout = tofile
print("%i base-%i digits of pi:\n" % (n, base))
print(intpart, ".\n")
display_fraction(d, skip, colwidth=10, columns=5)
if tofile:
sys.stdout = out_
print("\nFinished in %f seconds (%f calc, %f convert)" % \
((step1_time + step2_time), step1_time, step2_time))
def interactive():
print("Compute digits of pi with mpmath\n")
base = input("Which base? (2-36, 10 for decimal) \n> ")
digits = input("How many digits? (enter a big number, say, 10000)\n> ")
tofile = input("Output to file? (enter a filename, or just press " \
"enter\nto print directly to the screen) \n> ")
if tofile:
tofile = open(tofile, "w")
calculateit(int(base), int(digits), tofile)
input("\nPress enter to close this script.")
if __name__ == "__main__":
interactive()