Skip to content

Commit d225b1a

Browse files
NegaScoutstevengj
authored andcommitted
fix: Base.GMP.MPZ.invert yielding return code instead of return value (#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 ```
1 parent f929237 commit d225b1a

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

base/gmp.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ end
174174

175175
invert!(x::BigInt, a::BigInt, b::BigInt) =
176176
ccall((:__gmpz_invert, libgmp), Cint, (mpz_t, mpz_t, mpz_t), x, a, b)
177-
invert(a::BigInt, b::BigInt) = invert!(BigInt(), a, b)
178177
invert!(x::BigInt, b::BigInt) = invert!(x, x, b)
178+
invert(a::BigInt, b::BigInt) = (ret=BigInt(); invert!(ret, a, b); ret)
179179

180180
for op in (:add_ui, :sub_ui, :mul_ui, :mul_2exp, :fdiv_q_2exp, :pow_ui, :bin_ui)
181181
op! = Symbol(op, :!)

test/gmp.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,34 @@ end
468468
end
469469
end
470470

471+
@testset "modular invert" begin
472+
# test invert is correct and does not mutate
473+
a = BigInt(3)
474+
b = BigInt(7)
475+
i = BigInt(5)
476+
@test Base.GMP.MPZ.invert(a, b) == i
477+
@test a == BigInt(3)
478+
@test b == BigInt(7)
479+
480+
# test in place invert does mutate first argument
481+
a = BigInt(3)
482+
b = BigInt(7)
483+
i = BigInt(5)
484+
i_inplace = BigInt(3)
485+
Base.GMP.MPZ.invert!(i_inplace, b)
486+
@test i_inplace == i
487+
488+
# test in place invert does mutate only first argument
489+
a = BigInt(3)
490+
b = BigInt(7)
491+
i = BigInt(5)
492+
i_inplace = BigInt(0)
493+
Base.GMP.MPZ.invert!(i_inplace, a, b)
494+
@test i_inplace == i
495+
@test a == BigInt(3)
496+
@test b == BigInt(7)
497+
end
498+
471499
@testset "math ops returning BigFloat" begin
472500
# operations that when applied to Int64 give Float64, should give BigFloat
473501
@test typeof(exp(a)) == BigFloat

0 commit comments

Comments
 (0)