Skip to content

Commit 8f6f5aa

Browse files
fredrikekreStefanKarpinski
authored andcommitted
move mathematical constants to Base.MathConstants (#23427)
- only export pi, π and ℯ from Base - remove eu as alias for ℯ - add \euler as tab completion for ℯ
1 parent 9d6c9bd commit 8f6f5aa

20 files changed

+157
-131
lines changed

NEWS.md

+9
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,15 @@ Deprecated or removed
378378

379379
* `diagm(A::BitMatrix)` has been deprecated, use `diagm(vec(A))` instead ([#23373]).
380380

381+
* `` (written as `\mscre<TAB>` or `\euler<TAB>`) is the new default for Euler's
382+
number ([#23427]).
383+
384+
* The mathematical constants `π`, `pi`, ``, `e`, `γ`, `eulergamma`, `catalan`, `φ` and
385+
`golden` have been have been moved from `Base` to a new module; `Base.MathConstants`.
386+
Only `π`, `pi` and `` are now exported by default from `Base` ([#23427]).
387+
388+
* `eu` (previously an alias for ``) has been deprecated in favor of `` (or `MathConstants.e`) ([#23427]).
389+
381390
* `GMP.gmp_version()`, `GMP.GMP_VERSION`, `GMP.gmp_bits_per_limb()`, and `GMP.GMP_BITS_PER_LIBM`
382391
have been renamed to `GMP.version()`, `GMP.VERSION`, `GMP.bits_per_libm()`, and `GMP.BITS_PER_LIBM`,
383392
respectively. Similarly, `MPFR.get_version()`, has been renamed to `MPFR.version()` ([#23323]). Also,

base/deprecated.jl

+9
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,15 @@ export hex2num
16911691
@eval MPFR @deprecate get_version() version() false
16921692
@eval LinAlg.LAPACK @deprecate laver() version() false
16931693

1694+
# PR #23427
1695+
@deprecate_binding e ℯ
1696+
@deprecate_binding eu ℯ
1697+
@deprecate_binding γ MathConstants.γ
1698+
@deprecate_binding eulergamma MathConstants.eulergamma
1699+
@deprecate_binding catalan MathConstants.catalan
1700+
@deprecate_binding φ MathConstants.φ
1701+
@deprecate_binding golden MathConstants.golden
1702+
16941703
# PR #23271
16951704
function IOContext(io::IO; kws...)
16961705
depwarn("IOContext(io, k=v, ...) is deprecated, use IOContext(io, :k => v, ...) instead.", :IOContext)

base/exports.jl

+1-4
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,7 @@ export
186186
NaN64,
187187
im,
188188
π, pi,
189-
e, eu,
190-
γ, eulergamma,
191-
catalan,
192-
φ, golden,
189+
ℯ,
193190
I,
194191

195192
# Operators

base/irrationals.jl

-85
Original file line numberDiff line numberDiff line change
@@ -141,91 +141,6 @@ end
141141
big(x::Irrational) = convert(BigFloat,x)
142142
big(::Type{<:Irrational}) = BigFloat
143143

144-
## specific irrational mathematical constants
145-
146-
@irrational π 3.14159265358979323846 pi
147-
@irrational e 2.71828182845904523536 exp(big(1))
148-
@irrational γ 0.57721566490153286061 euler
149-
@irrational catalan 0.91596559417721901505 catalan
150-
@irrational φ 1.61803398874989484820 (1+sqrt(big(5)))/2
151-
152-
# aliases
153-
"""
154-
pi
155-
π
156-
157-
The constant pi.
158-
159-
```jldoctest
160-
julia> pi
161-
π = 3.1415926535897...
162-
```
163-
"""
164-
π, const pi = π
165-
166-
"""
167-
e
168-
eu
169-
170-
The constant e.
171-
172-
```jldoctest
173-
julia> e
174-
e = 2.7182818284590...
175-
```
176-
"""
177-
e, const eu = e
178-
179-
"""
180-
γ
181-
eulergamma
182-
183-
Euler's constant.
184-
185-
```jldoctest
186-
julia> eulergamma
187-
γ = 0.5772156649015...
188-
```
189-
"""
190-
γ, const eulergamma = γ
191-
192-
"""
193-
φ
194-
golden
195-
196-
The golden ratio.
197-
198-
```jldoctest
199-
julia> golden
200-
φ = 1.6180339887498...
201-
```
202-
"""
203-
φ, const golden = φ
204-
205-
"""
206-
catalan
207-
208-
Catalan's constant.
209-
210-
```jldoctest
211-
julia> catalan
212-
catalan = 0.9159655941772...
213-
```
214-
"""
215-
catalan
216-
217-
# special behaviors
218-
219-
# use exp for e^x or e.^x, as in
220-
# ^(::Irrational{:e}, x::Number) = exp(x)
221-
# but need to loop over types to prevent ambiguity with generic rules for ^(::Number, x) etc.
222-
for T in (Irrational, Rational, Integer, Number)
223-
^(::Irrational{:e}, x::T) = exp(x)
224-
end
225-
226-
log(::Irrational{:e}) = 1 # use 1 to correctly promote expressions like log(x)/log(e)
227-
log(::Irrational{:e}, x::Number) = log(x)
228-
229144
# align along = for nice Array printing
230145
function alignment(io::IO, x::Irrational)
231146
m = match(r"^(.*?)(=.*)$", sprint(0, showcompact, x, env=io))

base/linalg/lapack.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -5299,7 +5299,7 @@ for (bdsdc, elty) in
52995299
u, ldu, vt, ldvt,
53005300
q, iq, work, iwork, info)
53015301
chklapackerror(info[])
5302-
d, e, u, vt, q, iq
5302+
d, e_, u, vt, q, iq
53035303
end
53045304
end
53055305
end

base/mathconstants.jl

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# This file is a part of Julia. License is MIT: https://julialang.org/license
2+
3+
"""
4+
Base.MathConstants
5+
6+
Module containing the mathematical constants.
7+
See [`π`](@ref), [`ℯ`](@ref), [`γ`](@ref), [`φ`](@ref) and [`catalan`](@ref).
8+
"""
9+
module MathConstants
10+
11+
export π, pi, ℯ, e, γ, eulergamma, catalan, φ, golden
12+
13+
Base.@irrational π 3.14159265358979323846 pi
14+
Base.@irrational2.71828182845904523536 exp(big(1))
15+
Base.@irrational γ 0.57721566490153286061 euler
16+
Base.@irrational φ 1.61803398874989484820 (1+sqrt(big(5)))/2
17+
Base.@irrational catalan 0.91596559417721901505 catalan
18+
19+
# aliases
20+
"""
21+
π
22+
pi
23+
24+
The constant pi.
25+
26+
```jldoctest
27+
julia> pi
28+
π = 3.1415926535897...
29+
```
30+
"""
31+
π, const pi = π
32+
33+
"""
34+
35+
e
36+
37+
The constant ℯ.
38+
39+
```jldoctest
40+
julia> ℯ
41+
ℯ = 2.7182818284590...
42+
```
43+
"""
44+
ℯ, const e =
45+
46+
"""
47+
γ
48+
eulergamma
49+
50+
Euler's constant.
51+
52+
```jldoctest
53+
julia> MathConstants.eulergamma
54+
γ = 0.5772156649015...
55+
```
56+
"""
57+
γ, const eulergamma = γ
58+
59+
"""
60+
φ
61+
golden
62+
63+
The golden ratio.
64+
65+
```jldoctest
66+
julia> MathConstants.golden
67+
φ = 1.6180339887498...
68+
```
69+
"""
70+
φ, const golden = φ
71+
72+
"""
73+
catalan
74+
75+
Catalan's constant.
76+
77+
```jldoctest
78+
julia> MathConstants.catalan
79+
catalan = 0.9159655941772...
80+
```
81+
"""
82+
catalan
83+
84+
# loop over types to prevent ambiguities for ^(::Number, x)
85+
for T in (Irrational, Rational, Integer, Number)
86+
Base.:^(::Irrational{:ℯ}, x::T) = exp(x)
87+
end
88+
89+
Base.log(::Irrational{:ℯ}) = 1 # use 1 to correctly promote expressions like log(x)/log(ℯ)
90+
Base.log(::Irrational{:ℯ}, x::Number) = log(x)
91+
92+
end # module

base/repl/latex_symbols.jl

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ const latex_symbols = Dict(
8888
"\\implies" => "",
8989
"\\impliedby" => "",
9090
"\\to" => "",
91+
"\\euler" => "",
9192

9293
# Superscripts
9394
"\\^0" => "",

base/sysimg.jl

+2
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ include("hashing2.jl")
323323

324324
# irrational mathematical constants
325325
include("irrationals.jl")
326+
include("mathconstants.jl")
327+
using .MathConstants: ℯ, π, pi
326328

327329
# random number generation
328330
include("random/dSFMT.jl")

contrib/julia.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@
306306
<item> im </item>
307307
<item> π </item>
308308
<item> pi </item>
309+
<item> ℯ </item>
309310
<item> e </item>
310-
<item> eu </item>
311311
<item> γ </item>
312312
<item> eulergamma </item>
313313
<item> catalan </item>

doc/src/manual/variables.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ julia> pi
8181
π = 3.1415926535897...
8282
8383
julia> pi = 3
84-
ERROR: cannot assign variable Base.pi from module Main
84+
ERROR: cannot assign variable MathConstants.pi from module Main
8585
8686
julia> sqrt(100)
8787
10.0

doc/src/stdlib/numbers.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ Base.bytes2hex
6969
Base.one
7070
Base.oneunit
7171
Base.zero
72-
Base.pi
7372
Base.im
74-
Base.eu
75-
Base.catalan
76-
Base.eulergamma
77-
Base.golden
73+
Base.MathConstants.pi
74+
Base.MathConstants.ℯ
75+
Base.MathConstants.catalan
76+
Base.MathConstants.eulergamma
77+
Base.MathConstants.golden
7878
Base.Inf
7979
Base.Inf32
8080
Base.Inf16

test/bigfloat.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ for T in [Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt
77
@test big(2.0)^T(3) == 8
88
end
99

10-
for x in (2f0, pi, 7.8, big(e))
10+
for x in (2f0, pi, 7.8, big())
1111
@test big(typeof(x)) == typeof(big(x))
1212
@test big(typeof(complex(x, x))) == typeof(big(complex(x, x)))
1313
end

test/complex.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ end
901901
@test round.([1:5;] + 0.5im) == [1.0:5.0;]
902902

903903
@test float(Complex(1, 2)) == Complex(1.0, 2.0)
904-
@test round(float(Complex(π, e)),3) == Complex(3.142, 2.718)
904+
@test round(float(Complex(π, )),3) == Complex(3.142, 2.718)
905905
end
906906

907907
@testset "Complex32 arithmetic, PR #10003" begin
@@ -947,7 +947,7 @@ end
947947
@test big(1)/(10+10im) (5-5im)/big(100) big"0.05" - big"0.05"*im
948948

949949
@testset "Complex Irrationals, issue #21204" begin
950-
for x in (pi, e, catalan) # No need to test all of them
950+
for x in (pi, ℯ, Base.MathConstants.catalan) # No need to test all of them
951951
z = Complex(x, x)
952952
@test typeof(z) == Complex{typeof(x)}
953953
@test exp(z) exp(x) * cis(x)

test/core.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ end
942942

943943
@test unsafe_pointer_to_objref(ccall(:jl_call1, Ptr{Void}, (Any,Any),
944944
x -> x+1, 314158)) == 314159
945-
@test unsafe_pointer_to_objref(pointer_from_objref(e+pi)) == e+pi
945+
@test unsafe_pointer_to_objref(pointer_from_objref(+pi)) == +pi
946946

947947
let
948948
local a, aa

test/floatfuncs.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ for elty in (Float32,Float64)
6868
end
6969

7070
@testset "Types" begin
71-
for x in (Int16(0), 1, 2f0, pi, 3//4, big(5//6), 7.8, big(9), big(e))
71+
for x in (Int16(0), 1, 2f0, pi, 3//4, big(5//6), 7.8, big(9), big())
7272
@test float(typeof(x)) == typeof(float(x))
7373
@test float(typeof(complex(x, x))) == typeof(float(complex(x, x)))
7474
end

0 commit comments

Comments
 (0)