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

fixed the overflow problem of powermod(x::Integer, p::Integer, m::T) #48192

Merged
merged 14 commits into from
Jan 11, 2023
Merged

Conversation

StonerShaw
Copy link
Contributor

Fixed #48166

@oscardssmith
Copy link
Member

oscardssmith commented Jan 9, 2023

This isn't a great solution since it is type unstable and probably pretty slow. A better approach would probably be to compute powermod(invmod(x, m) -(p÷2), m)^2 % m. Also this should probably have a test.

@KristofferC
Copy link
Sponsor Member

This isn't a great solution since it is type unstable

I don't really see the type stability issue.

@oscardssmith
Copy link
Member

Good point. I forgot that the type of the exponent of powermod doesn't matter for the type of the result.

@StonerShaw
Copy link
Contributor Author

@oscardssmith @KristofferC Thanks! Maybe the BigInt can work, but I still think the fast exponentiation has higher performance and is more "elegant". I will revise it

base/intfuncs.jl Outdated Show resolved Hide resolved
base/intfuncs.jl Outdated Show resolved Hide resolved
base/intfuncs.jl Outdated Show resolved Hide resolved
@@ -267,6 +267,9 @@ end
@test powermod(2, -2, 5) == 4
@test powermod(2, -1, -5) == -2
@test powermod(2, -2, -5) == -1

@test powermod(2, typemin(Int128), 5) == 1
@test powermod(2, typemin(Int128), -5) == -4
Copy link
Member

Choose a reason for hiding this comment

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

add a test for BigInt power to make sure this still works for types without typemin.

@oscardssmith oscardssmith merged commit fec8304 into JuliaLang:master Jan 11, 2023
@oscardssmith
Copy link
Member

Thanks for the PR!

oscardssmith added a commit to oscardssmith/julia that referenced this pull request Jan 20, 2023
oscardssmith added a commit to oscardssmith/julia that referenced this pull request Jan 31, 2023
oscardssmith added a commit that referenced this pull request Feb 3, 2023
* type stability of powermod regressed in #48192
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

powermod(n, typemin(T), m) gives stack overflow.
4 participants