diff --git a/base/intfuncs.jl b/base/intfuncs.jl index e99b50a8ea689..42ffc46046823 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -165,6 +165,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 @@ -185,7 +187,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 diff --git a/test/math.jl b/test/math.jl index 9637a544441bf..eba7910098b4c 100644 --- a/test/math.jl +++ b/test/math.jl @@ -827,6 +827,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] diff --git a/test/replutil.jl b/test/replutil.jl index 54e1f2a7aaf2c..642d8bfe39ff6 100644 --- a/test/replutil.jl +++ b/test/replutil.jl @@ -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")