diff --git a/base/exports.jl b/base/exports.jl index 4563ccdefb455..8078cfd129702 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -186,10 +186,7 @@ export NaN64, im, π, pi, - e, eu, - γ, eulergamma, - catalan, - φ, golden, + ℯ, I, # Operators diff --git a/base/irrationals.jl b/base/irrationals.jl index 27b94a5968903..8f29f0d21b39e 100644 --- a/base/irrationals.jl +++ b/base/irrationals.jl @@ -142,91 +142,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)) diff --git a/base/linalg/lapack.jl b/base/linalg/lapack.jl index 58aea6a9519e9..2ff6719851a31 100644 --- a/base/linalg/lapack.jl +++ b/base/linalg/lapack.jl @@ -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 diff --git a/base/mathconstants.jl b/base/mathconstants.jl new file mode 100644 index 0000000000000..a053e346c9422 --- /dev/null +++ b/base/mathconstants.jl @@ -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.@irrational ℯ 2.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> 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 + +# 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 diff --git a/base/sysimg.jl b/base/sysimg.jl index 8b180d773cdef..676fb70a6ee53 100644 --- a/base/sysimg.jl +++ b/base/sysimg.jl @@ -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") diff --git a/test/bigfloat.jl b/test/bigfloat.jl index 4966c1b7a1c6e..4fa613ddb3718 100644 --- a/test/bigfloat.jl +++ b/test/bigfloat.jl @@ -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 diff --git a/test/complex.jl b/test/complex.jl index e1428a826573e..bf131ab6b6f93 100644 --- a/test/complex.jl +++ b/test/complex.jl @@ -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 @@ -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) diff --git a/test/core.jl b/test/core.jl index 08c3986562915..3517c3c6f147d 100644 --- a/test/core.jl +++ b/test/core.jl @@ -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 diff --git a/test/floatfuncs.jl b/test/floatfuncs.jl index c97f077f05438..ed1639b55ef10 100644 --- a/test/floatfuncs.jl +++ b/test/floatfuncs.jl @@ -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 diff --git a/test/math.jl b/test/math.jl index deba71da7dc5b..f3d31b1782939 100644 --- a/test/math.jl +++ b/test/math.jl @@ -23,16 +23,16 @@ end @testset "constants" begin - @test pi != e - @test e != 1//2 - @test 1//2 <= e - @test e <= 15//3 - @test big(1//2) < e - @test e < big(20//6) - @test e^pi == exp(pi) - @test e^2 == exp(2) - @test e^2.4 == exp(2.4) - @test e^(2//3) == exp(2//3) + @test pi != ℯ + @test ℯ != 1//2 + @test 1//2 <= ℯ + @test ℯ <= 15//3 + @test big(1//2) < ℯ + @test ℯ < big(20//6) + @test ℯ^pi == exp(pi) + @test ℯ^2 == exp(2) + @test ℯ^2.4 == exp(2.4) + @test ℯ^(2//3) == exp(2//3) @test Float16(3.0) < pi @test pi < Float16(4.0) @@ -171,19 +171,19 @@ end @test isequal(cos(T(0)), T(1)) @test cos(T(pi)/2) ≈ T(0) atol=eps(T) @test isequal(cos(T(pi)), T(-1)) - @test exp(T(1)) ≈ T(e) atol=10*eps(T) + @test exp(T(1)) ≈ T(ℯ) atol=10*eps(T) @test isequal(exp10(T(1)), T(10)) @test isequal(exp2(T(1)), T(2)) @test isequal(expm1(T(0)), T(0)) - @test expm1(T(1)) ≈ T(e)-1 atol=10*eps(T) + @test expm1(T(1)) ≈ T(ℯ)-1 atol=10*eps(T) @test isequal(hypot(T(3),T(4)), T(5)) @test isequal(log(T(1)), T(0)) - @test isequal(log(e,T(1)), T(0)) - @test log(T(e)) ≈ T(1) atol=eps(T) + @test isequal(log(ℯ,T(1)), T(0)) + @test log(T(ℯ)) ≈ T(1) atol=eps(T) @test isequal(log10(T(1)), T(0)) @test isequal(log10(T(10)), T(1)) @test isequal(log1p(T(0)), T(0)) - @test log1p(T(e)-1) ≈ T(1) atol=eps(T) + @test log1p(T(ℯ)-1) ≈ T(1) atol=eps(T) @test isequal(log2(T(1)), T(0)) @test isequal(log2(T(2)), T(1)) @test isequal(sin(T(0)), T(0)) @@ -402,7 +402,7 @@ end end @testset "Irrational args to sinpi/cospi/sinc/cosc" begin - for x in (pi, e, golden) + for x in (pi, ℯ, Base.MathConstants.golden) @test sinpi(x) ≈ Float64(sinpi(big(x))) @test cospi(x) ≈ Float64(cospi(big(x))) @test sinc(x) ≈ Float64(sinc(big(x))) @@ -662,8 +662,8 @@ end @testset "test fallback definitions" begin @test exp10(5) ≈ exp10(5.0) @test exp10(50//10) ≈ exp10(5.0) - @test log10(exp10(e)) ≈ e - @test log(e) === 1 + @test log10(exp10(ℯ)) ≈ ℯ + @test log(ℯ) === 1 @test exp2(Float16(2.0)) ≈ exp2(2.0) @test exp2(Float16(1.0)) === Float16(exp2(1.0)) @test exp10(Float16(1.0)) === Float16(exp10(1.0)) diff --git a/test/numbers.jl b/test/numbers.jl index 0f694fa410080..84e54e5e3b862 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -1,5 +1,6 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license +using Base.MathConstants const ≣ = isequal # convenient for comparing NaNs # basic booleans @@ -2491,7 +2492,7 @@ z2 = read(zbuf, Complex128) @test bswap(z2) === 3.5 - 4.5im #isreal(x::Real) = true -for x in [1.23, 7, e, 4//5] #[FP, Int, Irrational, Rat] +for x in [1.23, 7, ℯ, 4//5] #[FP, Int, Irrational, Rat] @test isreal(x) == true end @@ -2526,7 +2527,7 @@ let number_types = Set() end #getindex(x::Number) = x -for x in [1.23, 7, e, 4//5] #[FP, Int, Irrational, Rat] +for x in [1.23, 7, ℯ, 4//5] #[FP, Int, Irrational, Rat] @test getindex(x) == x @test getindex(x, 1, 1) == x end @@ -2537,7 +2538,7 @@ end #getindex(x::Array,-1) throws BoundsError #getindex(x::Array,0 throws BoundsError #getindex(x::Array,length(x::Array)+1) throws BoundsError -for x in [1.23, 7, e, 4//5] #[FP, Int, Irrational, Rat] +for x in [1.23, 7, ℯ, 4//5] #[FP, Int, Irrational, Rat] @test_throws BoundsError getindex(x,-1) @test_throws BoundsError getindex(x,0) @test_throws BoundsError getindex(x,2) @@ -2549,8 +2550,8 @@ end # copysign(x::Real, y::Real) = ifelse(signbit(x)!=signbit(y), -x, x) # flipsign(x::Real, y::Real) = ifelse(signbit(y), -x, x) -for x in [1.23, 7, e, 4//5] - for y in [1.23, 7, e, 4//5] +for x in [1.23, 7, ℯ, 4//5] + for y in [1.23, 7, ℯ, 4//5] @test copysign(x, y) == x @test copysign(x, -y) == -x @test copysign(-x, y) == x @@ -2570,7 +2571,7 @@ end #in(x::Number, y::Number) = x == y @test in(3,3) == true #Int @test in(2.0,2.0) == true #FP -@test in(e,e) == true #Const +@test in(ℯ,ℯ) == true #Const @test in(4//5,4//5) == true #Rat @test in(1+2im, 1+2im) == true #Imag @test in(3, 3.0) == true #mixed @@ -2952,7 +2953,7 @@ end end @test !iszero(nextfloat(BigFloat(0))) @test !isone(nextfloat(BigFloat(1))) - for x in (π, e, γ, catalan, φ) + for x in (π, ℯ, γ, catalan, φ) @test !iszero(x) @test !isone(x) end diff --git a/test/ranges.jl b/test/ranges.jl index 19e082e97819b..5e85e6e453654 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -1152,7 +1152,7 @@ end # test default values; n = 50, base = 10 @test logspace(a, b) == logspace(a, b, 50) == 10 .^ linspace(a, b, 50) @test logspace(a, b, n) == 10 .^ linspace(a, b, n) - for base in (10, 2, e) + for base in (10, 2, ℯ) @test logspace(a, b, base=base) == logspace(a, b, 50, base=base) == base.^linspace(a, b, 50) @test logspace(a, b, n, base=base) == base.^linspace(a, b, n) end diff --git a/test/rounding.jl b/test/rounding.jl index 32a19d10b1a1b..06256c8e72efd 100644 --- a/test/rounding.jl +++ b/test/rounding.jl @@ -1,7 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license # Small sanity tests to ensure changing the rounding of float functions work -using Base.Test +using Base: Test, MathConstants ## Float64 checks # a + b returns a number exactly between prevfloat(1.) and 1., so its @@ -110,7 +110,7 @@ end for T in [Float32,Float64] for v in [sqrt(big(2.0)),-big(1.0)/big(3.0),nextfloat(big(1.0)), prevfloat(big(1.0)),nextfloat(big(0.0)),prevfloat(big(0.0)), - pi,e,eulergamma,catalan,golden, + pi,ℯ,eulergamma,catalan,golden, typemax(Int64),typemax(UInt64),typemax(Int128),typemax(UInt128),0xa2f30f6001bb2ec6] pn = T(v,RoundNearest) @test pn == convert(T,BigFloat(v)) diff --git a/test/sparse/sparse.jl b/test/sparse/sparse.jl index 741ef0de794a8..15461daad25fc 100644 --- a/test/sparse/sparse.jl +++ b/test/sparse/sparse.jl @@ -428,7 +428,7 @@ end @testset "exp" begin A = sprandn(5,5,0.2) - @test e.^A ≈ e.^Array(A) + @test ℯ.^A ≈ ℯ.^Array(A) end @testset "reductions" begin