Skip to content

Commit

Permalink
move mathematical constants to Base.MathConstants (#23427)
Browse files Browse the repository at this point in the history
- only export pi, π and ℯ from Base
 - remove eu as alias for ℯ
 - add \euler as tab completion for ℯ
  • Loading branch information
fredrikekre authored and StefanKarpinski committed Aug 31, 2017
1 parent 9d6c9bd commit 8f6f5aa
Show file tree
Hide file tree
Showing 20 changed files with 157 additions and 131 deletions.
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,15 @@ Deprecated or removed

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

* `` (written as `\mscre<TAB>` or `\euler<TAB>`) is the new default for Euler's
number ([#23427]).

* The mathematical constants `π`, `pi`, ``, `e`, `γ`, `eulergamma`, `catalan`, `φ` and
`golden` have been have been moved from `Base` to a new module; `Base.MathConstants`.
Only `π`, `pi` and `` are now exported by default from `Base` ([#23427]).

* `eu` (previously an alias for ``) has been deprecated in favor of `` (or `MathConstants.e`) ([#23427]).

* `GMP.gmp_version()`, `GMP.GMP_VERSION`, `GMP.gmp_bits_per_limb()`, and `GMP.GMP_BITS_PER_LIBM`
have been renamed to `GMP.version()`, `GMP.VERSION`, `GMP.bits_per_libm()`, and `GMP.BITS_PER_LIBM`,
respectively. Similarly, `MPFR.get_version()`, has been renamed to `MPFR.version()` ([#23323]). Also,
Expand Down
9 changes: 9 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,15 @@ export hex2num
@eval MPFR @deprecate get_version() version() false
@eval LinAlg.LAPACK @deprecate laver() version() false

# PR #23427
@deprecate_binding e ℯ
@deprecate_binding eu ℯ
@deprecate_binding γ MathConstants.γ
@deprecate_binding eulergamma MathConstants.eulergamma
@deprecate_binding catalan MathConstants.catalan
@deprecate_binding φ MathConstants.φ
@deprecate_binding golden MathConstants.golden

# PR #23271
function IOContext(io::IO; kws...)
depwarn("IOContext(io, k=v, ...) is deprecated, use IOContext(io, :k => v, ...) instead.", :IOContext)
Expand Down
5 changes: 1 addition & 4 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,7 @@ export
NaN64,
im,
π, pi,
e, eu,
γ, eulergamma,
catalan,
φ, golden,
ℯ,
I,

# Operators
Expand Down
85 changes: 0 additions & 85 deletions base/irrationals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,91 +141,6 @@ end
big(x::Irrational) = convert(BigFloat,x)
big(::Type{<:Irrational}) = BigFloat

## specific irrational mathematical constants

@irrational π 3.14159265358979323846 pi
@irrational e 2.71828182845904523536 exp(big(1))
@irrational γ 0.57721566490153286061 euler
@irrational catalan 0.91596559417721901505 catalan
@irrational φ 1.61803398874989484820 (1+sqrt(big(5)))/2

# aliases
"""
pi
π
The constant pi.
```jldoctest
julia> pi
π = 3.1415926535897...
```
"""
π, const pi = π

"""
e
eu
The constant e.
```jldoctest
julia> e
e = 2.7182818284590...
```
"""
e, const eu = e

"""
γ
eulergamma
Euler's constant.
```jldoctest
julia> eulergamma
γ = 0.5772156649015...
```
"""
γ, const eulergamma = γ

"""
φ
golden
The golden ratio.
```jldoctest
julia> golden
φ = 1.6180339887498...
```
"""
φ, const golden = φ

"""
catalan
Catalan's constant.
```jldoctest
julia> catalan
catalan = 0.9159655941772...
```
"""
catalan

# special behaviors

# use exp for e^x or e.^x, as in
# ^(::Irrational{:e}, x::Number) = exp(x)
# but need to loop over types to prevent ambiguity with generic rules for ^(::Number, x) etc.
for T in (Irrational, Rational, Integer, Number)
^(::Irrational{:e}, x::T) = exp(x)
end

log(::Irrational{:e}) = 1 # use 1 to correctly promote expressions like log(x)/log(e)
log(::Irrational{:e}, x::Number) = log(x)

# align along = for nice Array printing
function alignment(io::IO, x::Irrational)
m = match(r"^(.*?)(=.*)$", sprint(0, showcompact, x, env=io))
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/lapack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5299,7 +5299,7 @@ for (bdsdc, elty) in
u, ldu, vt, ldvt,
q, iq, work, iwork, info)
chklapackerror(info[])
d, e, u, vt, q, iq
d, e_, u, vt, q, iq
end
end
end
Expand Down
92 changes: 92 additions & 0 deletions base/mathconstants.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

"""
Base.MathConstants
Module containing the mathematical constants.
See [`π`](@ref), [`ℯ`](@ref), [`γ`](@ref), [`φ`](@ref) and [`catalan`](@ref).
"""
module MathConstants

export π, pi, ℯ, e, γ, eulergamma, catalan, φ, golden

Base.@irrational π 3.14159265358979323846 pi
Base.@irrational2.71828182845904523536 exp(big(1))
Base.@irrational γ 0.57721566490153286061 euler
Base.@irrational φ 1.61803398874989484820 (1+sqrt(big(5)))/2
Base.@irrational catalan 0.91596559417721901505 catalan

# aliases
"""
π
pi
The constant pi.
```jldoctest
julia> pi
π = 3.1415926535897...
```
"""
π, const pi = π

"""
e
The constant ℯ.
```jldoctest
julia> ℯ
ℯ = 2.7182818284590...
```
"""
ℯ, const e =

"""
γ
eulergamma
Euler's constant.
```jldoctest
julia> MathConstants.eulergamma
γ = 0.5772156649015...
```
"""
γ, const eulergamma = γ

"""
φ
golden
The golden ratio.
```jldoctest
julia> MathConstants.golden
φ = 1.6180339887498...
```
"""
φ, const golden = φ

"""
catalan
Catalan's constant.
```jldoctest
julia> MathConstants.catalan
catalan = 0.9159655941772...
```
"""
catalan

# loop over types to prevent ambiguities for ^(::Number, x)
for T in (Irrational, Rational, Integer, Number)
Base.:^(::Irrational{:ℯ}, x::T) = exp(x)
end

Base.log(::Irrational{:ℯ}) = 1 # use 1 to correctly promote expressions like log(x)/log(ℯ)
Base.log(::Irrational{:ℯ}, x::Number) = log(x)

end # module
1 change: 1 addition & 0 deletions base/repl/latex_symbols.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const latex_symbols = Dict(
"\\implies" => "",
"\\impliedby" => "",
"\\to" => "",
"\\euler" => "",

# Superscripts
"\\^0" => "",
Expand Down
2 changes: 2 additions & 0 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ include("hashing2.jl")

# irrational mathematical constants
include("irrationals.jl")
include("mathconstants.jl")
using .MathConstants: ℯ, π, pi

# random number generation
include("random/dSFMT.jl")
Expand Down
2 changes: 1 addition & 1 deletion contrib/julia.xml
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@
<item> im </item>
<item> π </item>
<item> pi </item>
<item> ℯ </item>
<item> e </item>
<item> eu </item>
<item> γ </item>
<item> eulergamma </item>
<item> catalan </item>
Expand Down
2 changes: 1 addition & 1 deletion doc/src/manual/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ julia> pi
π = 3.1415926535897...
julia> pi = 3
ERROR: cannot assign variable Base.pi from module Main
ERROR: cannot assign variable MathConstants.pi from module Main
julia> sqrt(100)
10.0
Expand Down
10 changes: 5 additions & 5 deletions doc/src/stdlib/numbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ Base.bytes2hex
Base.one
Base.oneunit
Base.zero
Base.pi
Base.im
Base.eu
Base.catalan
Base.eulergamma
Base.golden
Base.MathConstants.pi
Base.MathConstants.ℯ
Base.MathConstants.catalan
Base.MathConstants.eulergamma
Base.MathConstants.golden
Base.Inf
Base.Inf32
Base.Inf16
Expand Down
2 changes: 1 addition & 1 deletion test/bigfloat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ for T in [Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt
@test big(2.0)^T(3) == 8
end

for x in (2f0, pi, 7.8, big(e))
for x in (2f0, pi, 7.8, big())
@test big(typeof(x)) == typeof(big(x))
@test big(typeof(complex(x, x))) == typeof(big(complex(x, x)))
end
Expand Down
4 changes: 2 additions & 2 deletions test/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ end
@test round.([1:5;] + 0.5im) == [1.0:5.0;]

@test float(Complex(1, 2)) == Complex(1.0, 2.0)
@test round(float(Complex(π, e)),3) == Complex(3.142, 2.718)
@test round(float(Complex(π, )),3) == Complex(3.142, 2.718)
end

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

@testset "Complex Irrationals, issue #21204" begin
for x in (pi, e, catalan) # No need to test all of them
for x in (pi, ℯ, Base.MathConstants.catalan) # No need to test all of them
z = Complex(x, x)
@test typeof(z) == Complex{typeof(x)}
@test exp(z) exp(x) * cis(x)
Expand Down
2 changes: 1 addition & 1 deletion test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ end

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

let
local a, aa
Expand Down
2 changes: 1 addition & 1 deletion test/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ for elty in (Float32,Float64)
end

@testset "Types" begin
for x in (Int16(0), 1, 2f0, pi, 3//4, big(5//6), 7.8, big(9), big(e))
for x in (Int16(0), 1, 2f0, pi, 3//4, big(5//6), 7.8, big(9), big())
@test float(typeof(x)) == typeof(float(x))
@test float(typeof(complex(x, x))) == typeof(float(complex(x, x)))
end
Expand Down
Loading

0 comments on commit 8f6f5aa

Please sign in to comment.