Skip to content

Commit

Permalink
(±1)^-n should not throw a DomainError
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Apr 22, 2015
1 parent a49ae53 commit d3e3615
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ function power_by_squaring(x, p::Integer)
elseif p == 2
return x*x
elseif p < 0
throw(DomainError())
let o = one(x)
x == o && return o
x == -1 && return iseven(p) ? o : copy(x)
throw(DomainError())
end
end
t = trailing_zeros(p) + 1
p >>= t
Expand Down
5 changes: 5 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2446,3 +2446,8 @@ for T in (Int8,Int16,Int32,Int64,Int128,UInt8,UInt16,UInt32,UInt64,UInt128)
@test_throws InexactError T(big(typemax(T))+1)
@test_throws InexactError T(big(typemin(T))-1)
end

# Integer exponentiation with negative powers (#8900)
@test_throws DomainError 2^(-3)
@test 1^(-3) == 1 == (-1)^(-4)
@test (-1)^(-3) == -1

0 comments on commit d3e3615

Please sign in to comment.