Python library for arbitrary-precision floating-point arithmetic
—
High-precision mathematical constants including π, e, and various special constants from number theory and analysis. All constants are computed with arbitrary precision matching the current context precision.
Basic mathematical constants that appear throughout mathematics.
# Fundamental constants (accessed as mp.constant_name)
mp.pi # π ≈ 3.14159265358979323846...
mp.e # Euler's number e ≈ 2.71828182845904523536...
mp.ln2 # Natural logarithm of 2 ≈ 0.69314718055994530942...
mp.ln10 # Natural logarithm of 10 ≈ 2.30258509299404568402...
mp.phi # Golden ratio φ = (1+√5)/2 ≈ 1.61803398874989484820...Important constants from number theory and analysis.
# Number theory and analysis constants
mp.euler # Euler-Mascheroni constant γ ≈ 0.57721566490153286061...
mp.catalan # Catalan's constant G ≈ 0.91596559417721901505...
mp.khinchin # Khinchin's constant K ≈ 2.68545200106530644531...
mp.glaisher # Glaisher-Kinkelin constant A ≈ 1.28242712910062263688...
mp.apery # Apéry's constant ζ(3) ≈ 1.20205690315959428540...Constants related to prime numbers and number theory.
# Prime number constants
mp.twinprime # Twin prime constant ≈ 0.66016181584686957392...
mp.mertens # Mertens constant M ≈ 0.26149721284764278375...Constants for angle conversion and unit systems.
# Unit conversion
mp.degree # π/180 for degree to radian conversion ≈ 0.01745329251994329577...Special numerical values used in computations.
# Special values
mp.inf # Positive infinity
mp.ninf # Negative infinity (-∞)
mp.nan # Not a number (NaN)
mp.j # Imaginary unit i = √(-1)
mp.eps # Machine epsilon (smallest representable positive number)All constants automatically adjust to the current precision setting and can be accessed as attributes of the mpmath context.
# Constants adapt to current precision
with mp.workdps(50):
high_precision_pi = mp.pi # π with 50 decimal places
with mp.workdps(100):
very_high_precision_e = mp.e # e with 100 decimal placesMathematical relationships between constants that can be verified with high precision.
import mpmath
from mpmath import mp
# Set different precision levels
mp.dps = 30
# Display fundamental constants
print("Fundamental Constants:")
print(f"π = {mp.pi}")
print(f"e = {mp.e}")
print(f"Golden ratio φ = {mp.phi}")
print(f"ln(2) = {mp.ln2}")
print(f"ln(10) = {mp.ln10}")
# Special constants
print("\nSpecial Constants:")
print(f"Euler-Mascheroni γ = {mp.euler}")
print(f"Catalan's constant = {mp.catalan}")
print(f"Khinchin's constant = {mp.khinchin}")
print(f"Glaisher-Kinkelin A = {mp.glaisher}")
print(f"Apéry's constant ζ(3) = {mp.apery}")
# Prime-related constants
print("\nPrime-Related Constants:")
print(f"Twin prime constant = {mp.twinprime}")
print(f"Mertens constant = {mp.mertens}")
# Unit conversion
print("\nUnit Conversion:")
print(f"Degrees to radians: π/180 = {mp.degree}")
angle_deg = 90
angle_rad = angle_deg * mp.degree
print(f"{angle_deg}° = {angle_rad} radians")
# Verify relationships
print("\nConstant Relationships:")
print(f"φ² - φ - 1 = {mp.phi**2 - mp.phi - 1}") # Should be 0
print(f"e^(iπ) + 1 = {mp.exp(mp.j * mp.pi) + 1}") # Euler's identity, should be 0
print(f"ζ(2) = π²/6 = {mp.pi**2 / 6}")
print(f"Actual ζ(2) = {mp.zeta(2)}")
# Precision scaling
print("\nPrecision Scaling:")
with mp.workdps(10):
pi_10 = mp.pi
print(f"π with 10 digits: {pi_10}")
with mp.workdps(50):
pi_50 = mp.pi
print(f"π with 50 digits: {pi_50}")
with mp.workdps(100):
pi_100 = mp.pi
print(f"π with 100 digits: {pi_100}")
# Special values
print("\nSpecial Values:")
print(f"Positive infinity: {mp.inf}")
print(f"Negative infinity: {mp.ninf}")
print(f"Not a number: {mp.nan}")
print(f"Imaginary unit: {mp.j}")
print(f"j² = {mp.j**2}") # Should be -1
# Machine epsilon
print(f"Machine epsilon: {mp.eps}")
# Using constants in calculations
print("\nCalculations with Constants:")
# Area of unit circle
area = mp.pi * 1**2
print(f"Area of unit circle: π = {area}")
# Compound interest formula: A = P*e^(rt)
principal = 1000
rate = 0.05 # 5%
time = 10
amount = principal * mp.exp(rate * time)
print(f"Compound interest: {principal} * e^({rate}*{time}) = {amount}")
# Golden ratio properties
print(f"φ = (1 + √5)/2 = {(1 + mp.sqrt(5))/2}")
print(f"1/φ = φ - 1 = {1/mp.phi} = {mp.phi - 1}")
# Trigonometric identities with high precision
print(f"sin²(π/6) + cos²(π/6) = {mp.sin(mp.pi/6)**2 + mp.cos(mp.pi/6)**2}")
print(f"e^(iπ/4) = {mp.exp(mp.j * mp.pi/4)}")
print(f"cos(π/4) + i*sin(π/4) = {mp.cos(mp.pi/4) + mp.j*mp.sin(mp.pi/4)}")
# Natural logarithm identities
print(f"e^(ln(2)) = {mp.exp(mp.ln2)}") # Should be 2
print(f"ln(e) = {mp.ln(mp.e)}") # Should be 1
print(f"ln(10) = {mp.ln10}")
print(f"ln(10)/ln(2) = {mp.ln10/mp.ln2}") # log₂(10)These constants have deep mathematical significance:
All constants are computed using mpmath's arbitrary precision arithmetic, ensuring accuracy to any requested precision level. The library uses efficient algorithms and series expansions optimized for high-precision computation.