Skip to content

Commit 492d10a

Browse files
alexp616fingolfinLilithHafner
authored
Fix incorrect result for Base.invmod(<:Signed, <:Unsigned) (#58010)
MWE of bug: ```julia julia> Int(invmod(1024, UInt(12289))) 5652 julia> invmod(1024, 12289) 12277 ``` --------- Co-authored-by: Max Horn <max@quendi.de> Co-authored-by: Lilith Orion Hafner <lilithhafner@gmail.com>
1 parent df91352 commit 492d10a

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

base/intfuncs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ function invmod(n::Integer, m::Integer)
286286
g, x, y = gcdx(n, m)
287287
g != 1 && throw(DomainError((n, m), LazyString("Greatest common divisor is ", g, ".")))
288288
# Note that m might be negative here.
289-
if n isa Unsigned && hastypemax(typeof(n)) && x > typemax(n)>>1
289+
if x isa Unsigned && hastypemax(typeof(x)) && x > typemax(x)>>1
290290
# x might have wrapped if it would have been negative
291291
# adding back m forces a correction
292292
x += m

test/intfuncs.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ end
258258
@test invmod(T(3), T(124))::T == 83
259259
end
260260

261+
for T in (Int8, Int16, Int32, Int64, Int128)
262+
@test invmod(T(3), unsigned(T)(124)) == 83
263+
end
264+
265+
# Verify issue described in PR 58010 is fixed
266+
@test invmod(UInt8(3), UInt16(50000)) === 0x411b
267+
261268
for T in (Int8, UInt8)
262269
for x in typemin(T):typemax(T)
263270
for m in typemin(T):typemax(T)

0 commit comments

Comments
 (0)