Skip to content

Commit

Permalink
move mathematical constants to Base.MathConstants
Browse files Browse the repository at this point in the history
 - only export pi, π and ℯ from Base
 - remove eu as alias for ℯ
  • Loading branch information
fredrikekre committed Aug 24, 2017
1 parent b46c74c commit c309521
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 124 deletions.
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 @@ -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))
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> 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
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 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
36 changes: 18 additions & 18 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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))
Expand Down
15 changes: 8 additions & 7 deletions test/numbers.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit c309521

Please sign in to comment.