How to perform high precision calculation involving 100s of decimal places?

1.7k Views Asked by At

This is not directly a math question but rather a question on computing error.

I am looking for ways to perform calculation involving 100s of decimal places (especially for, but not limited to, this problem statement)

One well known library for high precision calculation in Python is mpmath which is used for floating-point arithmetic with arbitrary precision. So to test its accuracy I calculated the value of $\zeta(2)$ to 200 decimal places using mpmath and compared the result with that from wolframalpha.com as shown in the code below. They agreed only to 16 decimal places. Comparing against the known value of $\pi$ to 200 decimal places, I found that wolframalpha.com was accurate to 200 decimal places whereas mpmath stops being accurate after the 16th decimal place. So clearly the arbitrary precision of mpmath is not at reliable. So mpmath is not as accurate as it is said to be.

Question: How do we calculate decimals to 100s decimal places accurately in pure Python (not SageMath?

Python Code:

%%time 
import mpmath

mp.dps = 200; mp.pretty = True

# Value from mpmath
z1 = zeta(2)

# Value from wolframalpha.com
z2 = 1.64493406684822643647241516664602518921894990120679843773555822937000747040320087383362890061975870530400431896233719067962872468700500778793510294633086627683173330936776260509525100687214005479681155879489036082327776191984075645587696323563670971009694890

# Error
z1 - z2
2

There are 2 best solutions below

1
On BEST ANSWER

Your z2 looks very precise, but it's actually just a Python float and so is truncated after around 16 dps. You need to do

z2 = mpf('1.64493406684822643647241516664602518921894990120679843773555822937000747040320087383362890061975870530400431896233719067962872468700500778793510294633086627683173330936776260509525100687214005479681155879489036082327776191984075645587696323563670971009694890')

to create an mpf object you can compare to zeta(2). (Also, like Sage, mpmath isn't "pure Python", in the sense that it is not part of the standard library).

5
On

Verifying a few of the responses you got:

from mpmath import mp

mp.dps = 200
mp.pretty = True

# Value from mpmath
z1 = mp.zeta(2)
print(f"z1 has type {type(z1)}")
print(f"zeta(2) from mpmath {z1}")

# Value from wolframalpha.com
z2 =  1.64493406684822643647241516664602518921894990120679843773555822937000747040320087383362890061975870530400431896233719067962872468700500778793510294633086627683173330936776260509525100687214005479681155879489036082327776191984075645587696323563670971009694890
print(f"z2 has type {type(z2)}")
print(f"z2 from copy-paste {z2}")

Output is:

z1 has type <class 'mpmath.ctx_mp_python.mpf'>
zeta(2) from mpmath 1.6449340668482264364724151666460251892189499012067984377355582293700074704032008738336289006197587053040043189623371906796287246870050077879351029463308662768317333093677626050952510068721400547968116
z2 has type <class 'float'>
z2 from copy-paste 1.6449340668482264