Skip to content

Commit d5d80c3

Browse files
committed
Deprecate Euler number e in favor of ℯ (U+212F)
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
1 parent 0d8cec3 commit d5d80c3

File tree

4 files changed

+59
-18
lines changed

4 files changed

+59
-18
lines changed

base/constants.jl

+10-10
Original file line numberDiff line numberDiff line change
@@ -99,33 +99,33 @@ big(x::MathConst) = convert(BigFloat,x)
9999
## specific mathematical constants
100100

101101
@math_const π 3.14159265358979323846 pi
102-
@math_const e 2.71828182845904523536 exp(big(1))
102+
@math_const 2.71828182845904523536 exp(big(1))
103103
@math_const γ 0.57721566490153286061 euler
104104
@math_const catalan 0.91596559417721901505 catalan
105105
@math_const φ 1.61803398874989484820 (1+sqrt(big(5)))/2
106106

107107
# aliases
108108
const pi = π
109-
const eu = e
109+
const eu =
110110
const eulergamma = γ
111111
const golden = φ
112112

113113
# special behaviors
114114

115-
# use exp for e^x or e.^x, as in
116-
# ^(::MathConst{:e}, x::Number) = exp(x)
117-
# .^(::MathConst{:e}, x) = exp(x)
115+
# use exp for ^x or .^x, as in
116+
# ^(::MathConst{:}, x::Number) = exp(x)
117+
# .^(::MathConst{:}, x) = exp(x)
118118
# but need to loop over types to prevent ambiguity with generic rules for ^(::Number, x) etc.
119119
for T in (MathConst, Rational, Integer, Number)
120-
^(::MathConst{:e}, x::T) = exp(x)
120+
^(::MathConst{:}, x::T) = exp(x)
121121
end
122122
for T in (Range, BitArray, SparseMatrixCSC, StridedArray, AbstractArray)
123-
.^(::MathConst{:e}, x::T) = exp(x)
123+
.^(::MathConst{:}, x::T) = exp(x)
124124
end
125-
^(::MathConst{:e}, x::AbstractMatrix) = expm(x)
125+
^(::MathConst{:}, x::AbstractMatrix) = expm(x)
126126

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

130130
#Align along = for nice Array printing
131131
function alignment(x::MathConst)

base/deprecated.jl

+41
Original file line numberDiff line numberDiff line change
@@ -522,3 +522,44 @@ export float32_isvalid, float64_isvalid
522522
@deprecate parseint(s,base) parse(Int, s, base)
523523
@deprecate parseint(T::Type, s) parse(T, s)
524524
@deprecate parseint(T::Type, s, base) parse(T, s, base)
525+
526+
# Deprecation of e MathConst
527+
export e
528+
const e = MathConst{:e}()
529+
530+
e_depwarn() = depwarn("e mathematical constant is deprecated, use ℯ (\\u212F) or eu instead", :e)
531+
532+
<(::MathConst{:e}, y::Rational{BigInt}) = (e_depwarn(); <(ℯ, y))
533+
<(x::Rational{BigInt}, ::MathConst{:e}) = (e_depwarn(); <(x, ℯ))
534+
<(::MathConst{:e}, y::Rational) = (e_depwarn(); <(ℯ, y))
535+
<(x::Rational, ::MathConst{:e}) =(e_depwarn(); <(x, ℯ))
536+
537+
<(x::MathConst{:e}, y::Rational{BigInt}) = (e_depwarn(); <(ℯ, y))
538+
<(x::Rational{BigInt}, y::MathConst{:e}) = (e_depwarn(); <(x, ℯ))
539+
540+
<=(x::MathConst{:e}, y::Rational) = (e_depwarn(); <=(ℯ, y))
541+
<=(x::Rational, y::MathConst{:e}) = (e_depwarn(); <=(x, ℯ))
542+
543+
hash(x::MathConst{:e}, h::UInt) = (e_depwarn(); hash(ℯ, h))
544+
545+
-(x::MathConst{:e}) = (e_depwarn(); -ℯ)
546+
for op in Symbol[:+, :-, :*, :/, :^]
547+
@eval $op(x::MathConst{:e}, y::MathConst{:e}) = (e_depwarn(); $op(ℯ, ℯ))
548+
end
549+
550+
convert(::Type{BigFloat}, ::MathConst{:e}) = (e_depwarn(); convert(BigFloat, ℯ))
551+
convert(::Type{Float64}, ::MathConst{:e}) = (e_depwarn(); convert(Float64, ℯ))
552+
convert(::Type{Float32}, ::MathConst{:e}) = (e_depwarn(); convert(Float32, ℯ))
553+
554+
big(x::MathConst{:e}) = (e_depwarn(); big(ℯ))
555+
556+
for T in (MathConst, Rational, Integer, Number)
557+
^(::MathConst{:e}, x::T) = (e_depwarn(); ^(ℯ, x))
558+
end
559+
for T in (Range, BitArray, SparseMatrixCSC, StridedArray, AbstractArray)
560+
.^(::MathConst{:e}, x::T) = (e_depwarn(); .^(ℯ, x))
561+
end
562+
^(::MathConst{:e}, x::AbstractMatrix) = (e_depwarn(); ^(ℯ, x))
563+
564+
log(::MathConst{:e}) = (e_depwarn(); log(ℯ))
565+
log(::MathConst{:e}, x) = (e_depwarn(); log(ℯ, x))

base/exports.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export
184184
NaN32,
185185
im,
186186
π, pi,
187-
e, eu,
187+
, eu,
188188
γ, eulergamma,
189189
catalan,
190190
φ, golden,

test/numbers.jl

+7-7
Original file line numberDiff line numberDiff line change
@@ -2320,7 +2320,7 @@ end
23202320
@test bswap(reinterpret(Float32,0x0000c03f)) === 1.5f0
23212321

23222322
#isreal(x::Real) = true
2323-
for x in [1.23, 7, e, 4//5] #[FP, Int, MathConst, Rat]
2323+
for x in [1.23, 7, , 4//5] #[FP, Int, MathConst, Rat]
23242324
@test isreal(x) == true
23252325
end
23262326

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

23372337
#getindex(x::Number) = x
2338-
for x in [1.23, 7, e, 4//5] #[FP, Int, MathConst, Rat]
2338+
for x in [1.23, 7, , 4//5] #[FP, Int, MathConst, Rat]
23392339
@test getindex(x) == x
23402340
end
23412341

23422342
#copysign(x::Real, y::Real) = ifelse(signbit(x)!=signbit(y), -x, x)
23432343
#same sign
2344-
for x in [1.23, 7, e, 4//5]
2345-
for y in [1.23, 7, e, 4//5]
2344+
for x in [1.23, 7, , 4//5]
2345+
for y in [1.23, 7, , 4//5]
23462346
@test copysign(x,y) == x
23472347
end
23482348
end
23492349
#different sign
2350-
for x in [1.23, 7, e, 4//5]
2351-
for y in [1.23, 7, e, 4//5]
2350+
for x in [1.23, 7, , 4//5]
2351+
for y in [1.23, 7, , 4//5]
23522352
@test copysign(x, -y) == -x
23532353
end
23542354
end
@@ -2361,7 +2361,7 @@ end
23612361
#in(x::Number, y::Number) = x == y
23622362
@test in(3,3) == true #Int
23632363
@test in(2.0,2.0) == true #FP
2364-
@test in(e,e) == true #Const
2364+
@test in(ℯ,ℯ) == true #Const
23652365
@test in(4//5,4//5) == true #Rat
23662366
@test in(1+2im, 1+2im) == true #Imag
23672367
@test in(3, 3.0) == true #mixed

0 commit comments

Comments
 (0)