Skip to content

Commit d7c3101

Browse files
Merge pull request #227 from araujoms/power
fix precision of non-integer power
2 parents 2ef0305 + 95c89e1 commit d7c3101

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

src/math/elementary/explog.jl

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,8 @@ function Base.:(^)(r::DoubleFloat{T}, n::Int) where {T<:IEEEFloat}
120120
end
121121

122122
function Base.:(^)(r::DoubleFloat{T}, n::DoubleFloat{T}) where {T <: IEEEFloat}
123-
if isinteger(n)
124-
return r^Int64(Float64(n)) # convert n to Float64 first
125-
else
126-
return exp(n * log(r))
127-
end
128-
end
129-
130-
function Base.:(^)(r::Int, n::DoubleFloat{T}) where {T<:IEEEFloat}
131-
if isinteger(n)
132-
return DoubleFloat{T}(r)^Int64(Float64(n)) # convert n to Float64 first
123+
if isinteger(n) && n min(2^20, maxintfloat(T, Int64))
124+
return r^Int64(n.hi)
133125
else
134126
return exp(n * log(r))
135127
end

test/function_accuracy.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ end
8585
@test test_atol(rand_bigf, rand_vals, atanh, 1.0e-31)
8686
@test test_rtol(rand_bigf, rand_vals, atanh, 1.0e-29)
8787

88+
@test test_atol(rand_bigf, rand_vals, x -> x^x, 1.0e-31)
89+
@test test_rtol(rand_bigf, rand_vals, x -> x^x, 1.0e-31)
90+
91+
@test test_atol(rand_bigf, rand_vals, x -> 2^x, 1.0e-31)
92+
@test test_rtol(rand_bigf, rand_vals, x -> 2^x, 1.0e-31)
93+
8894

8995
@test test_atol(rand1_bigf, rand1_vals, exp, 1.0e-30)
9096
@test test_rtol(rand1_bigf, rand1_vals, exp, 1.0e-31)
@@ -115,4 +121,10 @@ end
115121

116122
@test test_atol(rand1_bigf, rand1_vals, asinh, 1.0e-31)
117123
@test test_rtol(rand1_bigf, rand1_vals, asinh, 1.0e-31)
124+
125+
@test test_atol(rand1_bigf, rand1_vals, x -> x^x, 1.0e-30)
126+
@test test_rtol(rand1_bigf, rand1_vals, x -> x^x, 1.0e-31)
127+
128+
@test test_atol(rand1_bigf, rand1_vals, x -> 2^x, 1.0e-30)
129+
@test test_rtol(rand1_bigf, rand1_vals, x -> 2^x, 1.0e-31)
118130
end

0 commit comments

Comments
 (0)