Skip to content

Commit

Permalink
Deprecate Euler number e in favor of ℯ (U+212F)
Browse files Browse the repository at this point in the history
This reduces confusion with variables called e, and ℯ is
the Unicode character for "natural exponent". It also
makes this constant more similar to others, which have
both a "special letter" and longer ASCII form.

Cf. #10607
  • Loading branch information
nalimilan committed Mar 23, 2015
1 parent 0d8cec3 commit d5d80c3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 18 deletions.
20 changes: 10 additions & 10 deletions base/constants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,33 +99,33 @@ big(x::MathConst) = convert(BigFloat,x)
## specific mathematical constants

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

# aliases
const pi = π
const eu = e
const eu =
const eulergamma = γ
const golden = φ

# special behaviors

# use exp for e^x or e.^x, as in
# ^(::MathConst{:e}, x::Number) = exp(x)
# .^(::MathConst{:e}, x) = exp(x)
# use exp for ^x or .^x, as in
# ^(::MathConst{:}, x::Number) = exp(x)
# .^(::MathConst{:}, x) = exp(x)
# but need to loop over types to prevent ambiguity with generic rules for ^(::Number, x) etc.
for T in (MathConst, Rational, Integer, Number)
^(::MathConst{:e}, x::T) = exp(x)
^(::MathConst{:}, x::T) = exp(x)
end
for T in (Range, BitArray, SparseMatrixCSC, StridedArray, AbstractArray)
.^(::MathConst{:e}, x::T) = exp(x)
.^(::MathConst{:}, x::T) = exp(x)
end
^(::MathConst{:e}, x::AbstractMatrix) = expm(x)
^(::MathConst{:}, x::AbstractMatrix) = expm(x)

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

#Align along = for nice Array printing
function alignment(x::MathConst)
Expand Down
41 changes: 41 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,44 @@ export float32_isvalid, float64_isvalid
@deprecate parseint(s,base) parse(Int, s, base)
@deprecate parseint(T::Type, s) parse(T, s)
@deprecate parseint(T::Type, s, base) parse(T, s, base)

# Deprecation of e MathConst
export e
const e = MathConst{:e}()

e_depwarn() = depwarn("e mathematical constant is deprecated, use ℯ (\\u212F) or eu instead", :e)

<(::MathConst{:e}, y::Rational{BigInt}) = (e_depwarn(); <(ℯ, y))
<(x::Rational{BigInt}, ::MathConst{:e}) = (e_depwarn(); <(x, ℯ))
<(::MathConst{:e}, y::Rational) = (e_depwarn(); <(ℯ, y))
<(x::Rational, ::MathConst{:e}) =(e_depwarn(); <(x, ℯ))

<(x::MathConst{:e}, y::Rational{BigInt}) = (e_depwarn(); <(ℯ, y))
<(x::Rational{BigInt}, y::MathConst{:e}) = (e_depwarn(); <(x, ℯ))

<=(x::MathConst{:e}, y::Rational) = (e_depwarn(); <=(ℯ, y))
<=(x::Rational, y::MathConst{:e}) = (e_depwarn(); <=(x, ℯ))

hash(x::MathConst{:e}, h::UInt) = (e_depwarn(); hash(ℯ, h))

-(x::MathConst{:e}) = (e_depwarn(); -ℯ)
for op in Symbol[:+, :-, :*, :/, :^]
@eval $op(x::MathConst{:e}, y::MathConst{:e}) = (e_depwarn(); $op(ℯ, ℯ))
end

convert(::Type{BigFloat}, ::MathConst{:e}) = (e_depwarn(); convert(BigFloat, ℯ))
convert(::Type{Float64}, ::MathConst{:e}) = (e_depwarn(); convert(Float64, ℯ))
convert(::Type{Float32}, ::MathConst{:e}) = (e_depwarn(); convert(Float32, ℯ))

big(x::MathConst{:e}) = (e_depwarn(); big(ℯ))

for T in (MathConst, Rational, Integer, Number)
^(::MathConst{:e}, x::T) = (e_depwarn(); ^(ℯ, x))
end
for T in (Range, BitArray, SparseMatrixCSC, StridedArray, AbstractArray)
.^(::MathConst{:e}, x::T) = (e_depwarn(); .^(ℯ, x))
end
^(::MathConst{:e}, x::AbstractMatrix) = (e_depwarn(); ^(ℯ, x))

log(::MathConst{:e}) = (e_depwarn(); log(ℯ))
log(::MathConst{:e}, x) = (e_depwarn(); log(ℯ, x))
2 changes: 1 addition & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export
NaN32,
im,
π, pi,
e, eu,
, eu,
γ, eulergamma,
catalan,
φ, golden,
Expand Down
14 changes: 7 additions & 7 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2320,7 +2320,7 @@ end
@test bswap(reinterpret(Float32,0x0000c03f)) === 1.5f0

#isreal(x::Real) = true
for x in [1.23, 7, e, 4//5] #[FP, Int, MathConst, Rat]
for x in [1.23, 7, , 4//5] #[FP, Int, MathConst, Rat]
@test isreal(x) == true
end

Expand All @@ -2335,20 +2335,20 @@ for x in [subtypes(Complex); subtypes(Real)]
end

#getindex(x::Number) = x
for x in [1.23, 7, e, 4//5] #[FP, Int, MathConst, Rat]
for x in [1.23, 7, , 4//5] #[FP, Int, MathConst, Rat]
@test getindex(x) == x
end

#copysign(x::Real, y::Real) = ifelse(signbit(x)!=signbit(y), -x, x)
#same sign
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
end
end
#different sign
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
end
end
Expand All @@ -2361,7 +2361,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

0 comments on commit d5d80c3

Please sign in to comment.