Skip to content

Commit

Permalink
allow taking inverses mod 1
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Nov 15, 2013
1 parent 6a2141e commit 0cad721
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
5 changes: 4 additions & 1 deletion base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0cad721

Please sign in to comment.