Member-only story

Baking 1000 Digits of Pi from 3 Small Lines of Python

John Clark Craig
4 min readApr 16, 2021

Python’s mpmath module is a powerful tool for high accuracy calculation that you should know

You likely know that integers in Python can grow to any reasonable size. For example, here’s a short Python program that starts with 1 and multiplies by 256 with each step. After 20 steps the resulting integer is larger than many programming languages can handle:

n = 1
for i in range(20):
n *= 256
print(n)
# 256
# 65536
# 16777216
# 4294967296
# 1099511627776
# 281474976710656
# 72057594037927936
# 18446744073709551616
# 4722366482869645213696
# 1208925819614629174706176
# 309485009821345068724781056
# 79228162514264337593543950336
# 20282409603651670423947251286016
# 5192296858534827628530496329220096
# 1329227995784915872903807060280344576
# 340282366920938463463374607431768211456
# 87112285931760246646623899502532662132736
# 22300745198530623141535718272648361505980416
# 5708990770823839524233143877797980545530986496
# 1461501637330902918203684832716283019655932542976

This is very cool, but how about floating point numbers? Can we work with many digits after the decimal point? As with many challenges in Python, yes we can!

Mpmath to the Rescue

--

--

John Clark Craig
John Clark Craig

Written by John Clark Craig

Author, inventor, entrepreneur — passionate about alternate energy, technology, UFOs, and how Python programming can help you hack your life.

No responses yet