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

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

merged 3 commits into from
Aug 11, 2023

Conversation

nhz2
Copy link
Contributor

@nhz2 nhz2 commented Aug 7, 2023

This PR gives BigInt exponentiation the same edge-case behavior as other integer exponentiation.

The normal integer exponentiation checks if x is 1 or -1 before erroring if the power is negative.

julia/base/intfuncs.jl

Lines 283 to 287 in 9f9e989

elseif p < 0
isone(x) && return copy(x)
isone(-x) && return iseven(p) ? one(x) : copy(x)
throw_domerr_powbysq(x, p)
end

With this PR big(1)^big(-1) == 1, big(-1)^big(-1) == -1 and big(-1)^big(-2) == 1

@brenhinkeller brenhinkeller added the bignums BigInt and BigFloat label Aug 7, 2023
base/gmp.jl Outdated Show resolved Hide resolved
test/gmp.jl Outdated Show resolved Hide resolved
test/gmp.jl Outdated Show resolved Hide resolved
nhz2 and others added 2 commits August 8, 2023 21:49
Co-authored-by: Sukera <11753998+Seelengrab@users.noreply.github.com>
@test (big(i)^big(j)) % Int8 === int8_res
else
# Test both have exception of the same type
@test_throws typeof(int8_res) big(i)^big(j)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to my local tests, it is actually possible to check for int8_res directly, which would ensure the error message itself is also the same, helping ensure the error originated from the same location. Is there a reason you prefer only testing the type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error messages for BigInt and Int8 are currently different. This is because Int8 uses throw_domerr_powbysq(x, p) while BigInt has a custom message.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point.. Then this is good to go, I think.

@JeffBezanson JeffBezanson merged commit 3838dab into JuliaLang:master Aug 11, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bignums BigInt and BigFloat
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants