Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't throw DomainError for negative integer powers of ±1 #18342

Merged
merged 2 commits into from
Sep 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ function power_by_squaring(x_, p::Integer)
elseif p == 2
return x*x
elseif p < 0
x == 1 && return copy(x)
x == -1 && return iseven(p) ? one(x) : copy(x)
throw(DomainError())
end
t = trailing_zeros(p) + 1
Expand All @@ -135,7 +137,7 @@ function power_by_squaring(x_, p::Integer)
end
power_by_squaring(x::Bool, p::Unsigned) = ((p==0) | x)
function power_by_squaring(x::Bool, p::Integer)
p < 0 && throw(DomainError())
p < 0 && !x && throw(DomainError())
return (p==0) | x
end

Expand Down
4 changes: 4 additions & 0 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,10 @@ end
@test_throws DomainError 2 ^ -2
@test_throws DomainError (-2)^(2.2)
@test_throws DomainError (-2.0)^(2.2)
@test_throws DomainError false ^ -2
@test 1 ^ -2 === (-1) ^ -2 === 1
@test (-1) ^ -3 === -1
@test true ^ -2 === true

# issue #13748
let A = [1 2; 3 4]; B = [5 6; 7 8]; C = [9 10; 11 12]
Expand Down
2 changes: 1 addition & 1 deletion test/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ immutable TypeWithIntParam{T <: Integer} end
let undefvar
err_str = @except_strbt sqrt(-1) DomainError
@test contains(err_str, "Try sqrt(complex(x)).")
err_str = @except_strbt 1^(-1) DomainError
err_str = @except_strbt 2^(-1) DomainError
@test contains(err_str, "Cannot raise an integer x to a negative power -n")
err_str = @except_strbt (-1)^0.25 DomainError
@test contains(err_str, "Exponentiation yielding a complex result requires a complex argument")
Expand Down