From c7ae73caffb24947c29093c37be9e721fc23933d Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Mon, 5 Sep 2016 02:19:48 -0400 Subject: [PATCH] =?UTF-8?q?don't=20throw=20DomainError=20for=20negative=20?= =?UTF-8?q?integer=20powers=20of=20=C2=B11=20(#18342)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * don't throw DomainError for negative integer powers of ±1 * fix replutil test that was expecting 1^(-1) to fail --- base/intfuncs.jl | 4 +++- test/math.jl | 4 ++++ test/replutil.jl | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) 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")