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: Base.GMP.MPZ.invert yielding return code instead of return value #56894

Merged
merged 2 commits into from
Dec 24, 2024

Conversation

NegaScout
Copy link
Contributor

Hey,

I hope I followed all guidelines for contributing, this is my first contribution.

There is a bug in Base.GMP.MPZ.invert it returns GMP return code, instead of the actual value. This PR fixes it. I am not sure if it could be implemented better, but from my testing you cannot get away without a deepcopy.

Before:

julia> Base.GMP.MPZ.invert(big"3", big"7")
1

After:

julia> Base.GMP.MPZ.invert(big"3", big"7")
5

@NegaScout NegaScout changed the title fix: Base.GMP.MPZ.invert yielding return code instead of return value WIP: fix: Base.GMP.MPZ.invert yielding return code instead of return value Dec 23, 2024
@giordano giordano added bignums BigInt and BigFloat needs tests Unit tests are required for this change labels Dec 23, 2024
@giordano
Copy link
Contributor

It'd be good to add a regression test

base/gmp.jl Outdated
invert!(x::BigInt, b::BigInt) = invert!(x, x, b)
function invert(a::BigInt, b::BigInt)
ret = deepcopy(a)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
ret = deepcopy(a)
ret = BigInt()

A deepcopy is a bit excessive.

P.S.: Alternatively you can fix the invert!(x::BigInt, a::BigInt, b::BigInt) function to return x.

Copy link
Contributor Author

@NegaScout NegaScout Dec 23, 2024

Choose a reason for hiding this comment

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

I dont think I can without also refactoring invmod(x::BigInt, y::BigInt) at line 519

Copy link
Contributor Author

@NegaScout NegaScout Dec 23, 2024

Choose a reason for hiding this comment

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

However I modified the original commit in shorter notation without the deepcopy

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively you can fix the invert!(x::BigInt, a::BigInt, b::BigInt) function to return x.

Initially I agreed, but looking at invmod, I see that there still needs to be a way to get the return code and the return value is the cleanest way I can think of.

@NegaScout NegaScout force-pushed the jw/variable_names_refactor branch from e4c2c59 to 00cda38 Compare December 23, 2024 17:10
@giordano giordano removed the needs tests Unit tests are required for this change label Dec 23, 2024
test/gmp.jl Outdated Show resolved Hide resolved
@NegaScout NegaScout force-pushed the jw/variable_names_refactor branch from 9eb202f to bb79ec4 Compare December 23, 2024 21:41
@NegaScout NegaScout changed the title WIP: fix: Base.GMP.MPZ.invert yielding return code instead of return value fix: Base.GMP.MPZ.invert yielding return code instead of return value Dec 24, 2024
@LilithHafner LilithHafner added the bugfix This change fixes an existing bug label Dec 24, 2024
Copy link
Member

@LilithHafner LilithHafner left a comment

Choose a reason for hiding this comment

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

Thanks for finding and fixing this! A lovely first pull request.

@LilithHafner LilithHafner merged commit cab11bb into JuliaLang:master Dec 24, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bignums BigInt and BigFloat bugfix This change fixes an existing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants