Skip to content

Commit 77a8888

Browse files
MathConst special behavior: e^x = exp(x)
Based on suggestion by @stevengj: 43f9731#commitcomment-3399288 One problem here is that e^1 != e: julia> e^1 == e false If you convert to float instead of calling exp, it's fine: julia> float(e)^1 == e true This seems more like a problem with the exp function than with this particular special behavior, but it is a bit of an issue.
1 parent 4d64eb5 commit 77a8888

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

base/constants.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,10 @@ const eu = e
6767
const eulergamma = γ
6868
const catalan = G
6969
const golden = φ
70+
71+
# special behaviors
72+
73+
for T in {MathConst, Rational, Integer, Number,
74+
Ranges, BitArray, SparseMatrixCSC, AbstractArray}
75+
^(::MathConst{:e}, x::T) = exp(x)
76+
end

base/rational.jl

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,13 @@ for f in (:int8, :int16, :int32, :int64, :int128,
181181
@eval ($f)(x::Rational) = ($f)(iround(x))
182182
end
183183

184-
function ^(x::Rational, y::Integer)
185-
if y < 0
186-
Rational(x.den^-y, x.num^-y)
187-
else
188-
Rational(x.num^y, x.den^y)
189-
end
190-
end
184+
^(x::Rational, y::Integer) = y < 0 ?
185+
Rational(x.den^-y, x.num^-y) : Rational(x.num^y, x.den^y)
191186

192187
^(x::Number, y::Rational) = x^(y.num/y.den)
193188
^{T<:FloatingPoint}(x::T, y::Rational) = x^(convert(T,y.num)/y.den)
194189
^{T<:FloatingPoint}(x::Complex{T}, y::Rational) = x^(convert(T,y.num)/y.den)
195190

196-
^{T<:Rational}(z::Complex{T}, n::Bool) = n ? z : one(z) # to resolve ambiguity
197-
191+
^{T<:Rational}(z::Complex{T}, n::Bool) = n ? z : one(z) # to resolve ambiguity
198192
^{T<:Rational}(z::Complex{T}, n::Integer) =
199193
n>=0 ? power_by_squaring(z,n) : power_by_squaring(inv(z),-n)

0 commit comments

Comments
 (0)