Skip to content

Commit

Permalink
fix: Base.GMP.MPZ.invert yielding return code instead of return value (
Browse files Browse the repository at this point in the history
…#56894)

There is a bug in `Base.GMP.MPZ.invert` it returned GMP return code,
instead of the actual value. This commit fixes it.

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

After:
```
julia> Base.GMP.MPZ.invert(big"3", big"7")
5
```
  • Loading branch information
NegaScout authored Dec 24, 2024
1 parent 64d37f5 commit cab11bb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ end

invert!(x::BigInt, a::BigInt, b::BigInt) =
ccall((:__gmpz_invert, libgmp), Cint, (mpz_t, mpz_t, mpz_t), x, a, b)
invert(a::BigInt, b::BigInt) = invert!(BigInt(), a, b)
invert!(x::BigInt, b::BigInt) = invert!(x, x, b)
invert(a::BigInt, b::BigInt) = (ret=BigInt(); invert!(ret, a, b); ret)

for op in (:add_ui, :sub_ui, :mul_ui, :mul_2exp, :fdiv_q_2exp, :pow_ui, :bin_ui)
op! = Symbol(op, :!)
Expand Down
28 changes: 28 additions & 0 deletions test/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,34 @@ end
end
end

@testset "modular invert" begin
# test invert is correct and does not mutate
a = BigInt(3)
b = BigInt(7)
i = BigInt(5)
@test Base.GMP.MPZ.invert(a, b) == i
@test a == BigInt(3)
@test b == BigInt(7)

# test in place invert does mutate first argument
a = BigInt(3)
b = BigInt(7)
i = BigInt(5)
i_inplace = BigInt(3)
Base.GMP.MPZ.invert!(i_inplace, b)
@test i_inplace == i

# test in place invert does mutate only first argument
a = BigInt(3)
b = BigInt(7)
i = BigInt(5)
i_inplace = BigInt(0)
Base.GMP.MPZ.invert!(i_inplace, a, b)
@test i_inplace == i
@test a == BigInt(3)
@test b == BigInt(7)
end

@testset "math ops returning BigFloat" begin
# operations that when applied to Int64 give Float64, should give BigFloat
@test typeof(exp(a)) == BigFloat
Expand Down

0 comments on commit cab11bb

Please sign in to comment.