diff --git a/base/gmp.jl b/base/gmp.jl index 0734e19c87b8a..f28ff9907584f 100644 --- a/base/gmp.jl +++ b/base/gmp.jl @@ -167,7 +167,10 @@ end function invmod(x::BigInt, y::BigInt) z = BigInt() y = abs(y) - if (y<2 || ccall((:__gmpz_invert, :libgmp), Cint, (Ptr{BigInt}, Ptr{BigInt}, Ptr{BigInt}), &z, &x, &y) == 0) + if y == 1 + return big(0) + end + if (y==0 || ccall((:__gmpz_invert, :libgmp), Cint, (Ptr{BigInt}, Ptr{BigInt}, Ptr{BigInt}), &z, &x, &y) == 0) error("no inverse exists") end return z diff --git a/base/intfuncs.jl b/base/intfuncs.jl index d31adf4178847..283201e3e1007 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -68,7 +68,7 @@ gcdx(a::Integer, b::Integer) = gcdx(promote(a,b)...) # multiplicative inverse of n mod m, error if none function invmod(n, m) g, x, y = gcdx(n, m) - if g != 1 || m == 0 || m == 1 || m == -1 + if g != 1 || m == 0 error("no inverse exists") end x < 0 ? abs(m) + x : x diff --git a/test/numbers.jl b/test/numbers.jl index 8c3acbb270a0b..904dd3f4bc72f 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -1522,7 +1522,7 @@ for i = -20:20, j = -20:20 @test d == gcd(ib,jb) @test lcm(i,j) == lcm(ib,jb) @test gcdx(i,j) == gcdx(ib,jb) - if abs(j) < 2 + if j == 0 @test_throws invmod(i,j) @test_throws invmod(ib,jb) elseif d == 1