Skip to content

Commit

Permalink
bugfix for lcm(0,0), add test coverage of gcd-related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Nov 14, 2013
1 parent dc8652c commit ef40cd9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
4 changes: 3 additions & 1 deletion base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ function gcd{T<:Integer}(a::T, b::T)
end
abs(a)
end
lcm{T<:Integer}(a::T, b::T) = abs(a * div(b, gcd(b,a)))

# explicit a==0 test is to handle case of lcm(0,0) correctly
lcm{T<:Integer}(a::T, b::T) = a == 0 ? a : abs(a * div(b, gcd(b,a)))

gcd(a::Integer) = a
lcm(a::Integer) = a
Expand Down
4 changes: 2 additions & 2 deletions doc/helpdb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3885,13 +3885,13 @@ popdisplay(d::Display)

("Mathematical Functions","Base","gcd","gcd(x, y)
Greatest common (positive) divisor
Greatest common (positive) divisor (or zero if x and y are both zero).
"),

("Mathematical Functions","Base","lcm","lcm(x, y)
Least common (positive) multiple
Least common (non-negative) multiple.
"),

Expand Down
4 changes: 2 additions & 2 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2632,11 +2632,11 @@ Mathematical Functions

.. function:: gcd(x,y)

Greatest common (positive) divisor.
Greatest common (positive) divisor (or zero if x and y are both zero).

.. function:: lcm(x,y)

Least common (positive) multiple.
Least common (non-negative) multiple.

.. function:: gcdx(x,y)

Expand Down
13 changes: 13 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1511,3 +1511,16 @@ end
# overflow in rational comparison
@test 3//2 < typemax(Int)
@test 3//2 <= typemax(Int)

# check gcd and related functions against GMP
for i = -20:20, j = -20:20
local d = gcd(i,j)
@test d >= 0
@test lcm(i,j) >= 0
local ib = big(i)
local jb = big(j)
@test d == gcd(ib,jb)
@test lcm(i,j) == lcm(ib,jb)
@test gcdx(i,j) == gcdx(ib,jb)
@test d != 1 || invmod(i,j) == invmod(ib,jb)
end

0 comments on commit ef40cd9

Please sign in to comment.