0
# Mathematical Constants
1
2
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.
3
4
## Capabilities
5
6
### Fundamental Constants
7
8
Basic mathematical constants that appear throughout mathematics.
9
10
```python { .api }
11
# Fundamental constants (accessed as mp.constant_name)
12
mp.pi # π ≈ 3.14159265358979323846...
13
mp.e # Euler's number e ≈ 2.71828182845904523536...
14
mp.ln2 # Natural logarithm of 2 ≈ 0.69314718055994530942...
15
mp.ln10 # Natural logarithm of 10 ≈ 2.30258509299404568402...
16
mp.phi # Golden ratio φ = (1+√5)/2 ≈ 1.61803398874989484820...
17
```
18
19
### Special Mathematical Constants
20
21
Important constants from number theory and analysis.
22
23
```python { .api }
24
# Number theory and analysis constants
25
mp.euler # Euler-Mascheroni constant γ ≈ 0.57721566490153286061...
26
mp.catalan # Catalan's constant G ≈ 0.91596559417721901505...
27
mp.khinchin # Khinchin's constant K ≈ 2.68545200106530644531...
28
mp.glaisher # Glaisher-Kinkelin constant A ≈ 1.28242712910062263688...
29
mp.apery # Apéry's constant ζ(3) ≈ 1.20205690315959428540...
30
```
31
32
### Prime-Related Constants
33
34
Constants related to prime numbers and number theory.
35
36
```python { .api }
37
# Prime number constants
38
mp.twinprime # Twin prime constant ≈ 0.66016181584686957392...
39
mp.mertens # Mertens constant M ≈ 0.26149721284764278375...
40
```
41
42
### Angular and Unit Conversion
43
44
Constants for angle conversion and unit systems.
45
46
```python { .api }
47
# Unit conversion
48
mp.degree # π/180 for degree to radian conversion ≈ 0.01745329251994329577...
49
```
50
51
### Special Values
52
53
Special numerical values used in computations.
54
55
```python { .api }
56
# Special values
57
mp.inf # Positive infinity
58
mp.ninf # Negative infinity (-∞)
59
mp.nan # Not a number (NaN)
60
mp.j # Imaginary unit i = √(-1)
61
mp.eps # Machine epsilon (smallest representable positive number)
62
```
63
64
### Accessing Constants
65
66
All constants automatically adjust to the current precision setting and can be accessed as attributes of the mpmath context.
67
68
```python { .api }
69
# Constants adapt to current precision
70
with mp.workdps(50):
71
high_precision_pi = mp.pi # π with 50 decimal places
72
73
with mp.workdps(100):
74
very_high_precision_e = mp.e # e with 100 decimal places
75
```
76
77
### Constant Relationships
78
79
Mathematical relationships between constants that can be verified with high precision.
80
81
## Usage Examples
82
83
```python
84
import mpmath
85
from mpmath import mp
86
87
# Set different precision levels
88
mp.dps = 30
89
90
# Display fundamental constants
91
print("Fundamental Constants:")
92
print(f"π = {mp.pi}")
93
print(f"e = {mp.e}")
94
print(f"Golden ratio φ = {mp.phi}")
95
print(f"ln(2) = {mp.ln2}")
96
print(f"ln(10) = {mp.ln10}")
97
98
# Special constants
99
print("\nSpecial Constants:")
100
print(f"Euler-Mascheroni γ = {mp.euler}")
101
print(f"Catalan's constant = {mp.catalan}")
102
print(f"Khinchin's constant = {mp.khinchin}")
103
print(f"Glaisher-Kinkelin A = {mp.glaisher}")
104
print(f"Apéry's constant ζ(3) = {mp.apery}")
105
106
# Prime-related constants
107
print("\nPrime-Related Constants:")
108
print(f"Twin prime constant = {mp.twinprime}")
109
print(f"Mertens constant = {mp.mertens}")
110
111
# Unit conversion
112
print("\nUnit Conversion:")
113
print(f"Degrees to radians: π/180 = {mp.degree}")
114
angle_deg = 90
115
angle_rad = angle_deg * mp.degree
116
print(f"{angle_deg}° = {angle_rad} radians")
117
118
# Verify relationships
119
print("\nConstant Relationships:")
120
print(f"φ² - φ - 1 = {mp.phi**2 - mp.phi - 1}") # Should be 0
121
print(f"e^(iπ) + 1 = {mp.exp(mp.j * mp.pi) + 1}") # Euler's identity, should be 0
122
print(f"ζ(2) = π²/6 = {mp.pi**2 / 6}")
123
print(f"Actual ζ(2) = {mp.zeta(2)}")
124
125
# Precision scaling
126
print("\nPrecision Scaling:")
127
with mp.workdps(10):
128
pi_10 = mp.pi
129
print(f"π with 10 digits: {pi_10}")
130
131
with mp.workdps(50):
132
pi_50 = mp.pi
133
print(f"π with 50 digits: {pi_50}")
134
135
with mp.workdps(100):
136
pi_100 = mp.pi
137
print(f"π with 100 digits: {pi_100}")
138
139
# Special values
140
print("\nSpecial Values:")
141
print(f"Positive infinity: {mp.inf}")
142
print(f"Negative infinity: {mp.ninf}")
143
print(f"Not a number: {mp.nan}")
144
print(f"Imaginary unit: {mp.j}")
145
print(f"j² = {mp.j**2}") # Should be -1
146
147
# Machine epsilon
148
print(f"Machine epsilon: {mp.eps}")
149
150
# Using constants in calculations
151
print("\nCalculations with Constants:")
152
# Area of unit circle
153
area = mp.pi * 1**2
154
print(f"Area of unit circle: π = {area}")
155
156
# Compound interest formula: A = P*e^(rt)
157
principal = 1000
158
rate = 0.05 # 5%
159
time = 10
160
amount = principal * mp.exp(rate * time)
161
print(f"Compound interest: {principal} * e^({rate}*{time}) = {amount}")
162
163
# Golden ratio properties
164
print(f"φ = (1 + √5)/2 = {(1 + mp.sqrt(5))/2}")
165
print(f"1/φ = φ - 1 = {1/mp.phi} = {mp.phi - 1}")
166
167
# Trigonometric identities with high precision
168
print(f"sin²(π/6) + cos²(π/6) = {mp.sin(mp.pi/6)**2 + mp.cos(mp.pi/6)**2}")
169
print(f"e^(iπ/4) = {mp.exp(mp.j * mp.pi/4)}")
170
print(f"cos(π/4) + i*sin(π/4) = {mp.cos(mp.pi/4) + mp.j*mp.sin(mp.pi/4)}")
171
172
# Natural logarithm identities
173
print(f"e^(ln(2)) = {mp.exp(mp.ln2)}") # Should be 2
174
print(f"ln(e) = {mp.ln(mp.e)}") # Should be 1
175
print(f"ln(10) = {mp.ln10}")
176
print(f"ln(10)/ln(2) = {mp.ln10/mp.ln2}") # log₂(10)
177
```
178
179
### Historical and Mathematical Context
180
181
These constants have deep mathematical significance:
182
183
- **π (pi)**: Ratio of circumference to diameter of a circle, appears throughout geometry and analysis
184
- **e**: Base of natural logarithm, fundamental in calculus and probability
185
- **γ (Euler-Mascheroni)**: Appears in number theory and analysis, related to harmonic numbers
186
- **φ (Golden ratio)**: Appears in geometry, art, and nature; satisfies φ² = φ + 1
187
- **G (Catalan)**: Arises in combinatorics and number theory
188
- **K (Khinchin)**: Related to continued fraction expansions
189
- **A (Glaisher-Kinkelin)**: Appears in various mathematical series and products
190
- **ζ(3) (Apéry)**: Value of Riemann zeta function at 3, proved irrational by Apéry
191
192
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.