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

Fix big(1)^big(-1) #50821

Merged
merged 3 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 2 deletions base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -628,11 +628,11 @@ isqrt(x::BigInt) = MPZ.sqrt(x)
^(x::BigInt, y::Culong) = MPZ.pow_ui(x, y)

function bigint_pow(x::BigInt, y::Integer)
if x== 1; return x; end
if x==-1; return isodd(y) ? x : -x; end
nhz2 marked this conversation as resolved.
Show resolved Hide resolved
if y<0; throw(DomainError(y, "`y` cannot be negative.")); end
@noinline throw1(y) =
throw(OverflowError("exponent $y is too large and computation will overflow"))
if x== 1; return x; end
if x==-1; return isodd(y) ? x : -x; end
if y>typemax(Culong)
x==0 && return x

Expand Down
34 changes: 32 additions & 2 deletions test/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,38 @@ end
@test isqrt(big(4)) == 2
@test isqrt(big(5)) == 2

@test big(5)^true == big(5)
@test big(5)^false == one(BigInt)

@testset "Exponentiation operator" begin
@test big(5)^true == big(5)
@test big(5)^false == one(BigInt)
# Test BigInt and Int8 exponentiation are consistent.
for i = typemin(Int8):typemax(Int8), j = typemin(Int8):typemax(Int8)
nhz2 marked this conversation as resolved.
Show resolved Hide resolved
int8_res = try
i^j
catch e
e
end
big_res = try
big(i)^big(j)
catch e
e
end
if int8_res isa Exception && big_res isa Exception
# Both have exception
if typeof(int8_res) != typeof(big_res)
throw(big_res)
end
elseif big_res isa Exception
@error "$i^$j failed on BigInt but not Int8"
throw(big_res)
elseif int8_res isa Exception
@error "$i^$j failed on Int8 but not BigInt"
throw(int8_res)
else
@test big_res%Int8 === int8_res
end
end
end
nhz2 marked this conversation as resolved.
Show resolved Hide resolved

@testset "math ops returning BigFloat" begin
# operations that when applied to Int64 give Float64, should give BigFloat
Expand Down