Skip to content

Commit

Permalink
gcd and lcm fixed for one negative argument (#40968)
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausC authored May 29, 2021
1 parent a5889a8 commit fb42ea5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
5 changes: 3 additions & 2 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ function lcm(a::T, b::T) where T<:Integer
end
end

gcd(a::Union{Integer,Rational}) = a
lcm(a::Union{Integer,Rational}) = a
gcd(a::Integer) = checked_abs(a)
gcd(a::Rational) = checked_abs(a.num) // a.den
lcm(a::Union{Integer,Rational}) = gcd(a)
gcd(a::Unsigned, b::Signed) = gcd(promote(a, abs(b))...)
gcd(a::Signed, b::Unsigned) = gcd(promote(abs(a), b)...)
gcd(a::Real, b::Real) = gcd(promote(a,b)...)
Expand Down
5 changes: 4 additions & 1 deletion test/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ using Random
@test gcd(T(0), T(15)) === T(15)
@test gcd(T(15), T(0)) === T(15)
if T <: Signed
@test gcd(T(-12)) === T(12)
@test gcd(T(0), T(-15)) === T(15)
@test gcd(T(-15), T(0)) === T(15)
@test gcd(T(3), T(-15)) === T(3)
Expand Down Expand Up @@ -78,6 +79,7 @@ using Random
@test lcm(T(0), T(3)) === T(0)
@test lcm(T(0), T(0)) === T(0)
if T <: Signed
@test lcm(T(-12)) === T(12)
@test lcm(T(0), T(-4)) === T(0)
@test lcm(T(-4), T(0)) === T(0)
@test lcm(T(4), T(-6)) === T(12)
Expand Down Expand Up @@ -154,6 +156,7 @@ end
@test gcd(T[3, 15]) === T(3)
@test gcd(T[0, 15]) === T(15)
if T <: Signed
@test gcd(T[-12]) === T(12)
@test gcd(T[3,-15]) === T(3)
@test gcd(T[-3,-15]) === T(3)
end
Expand All @@ -163,12 +166,12 @@ end
@test gcd(T[2, 4, 3, 5]) === T(1)

@test lcm(T[]) === T(1)
@test lcm(T[2]) === T(2)
@test lcm(T[2, 3]) === T(6)
@test lcm(T[4, 6]) === T(12)
@test lcm(T[3, 0]) === T(0)
@test lcm(T[0, 0]) === T(0)
if T <: Signed
@test lcm(T[-2]) === T(2)
@test lcm(T[4, -6]) === T(12)
@test lcm(T[-4, -6]) === T(12)
end
Expand Down
2 changes: 2 additions & 0 deletions test/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ end
@test gcd(b, a) === T(2)//T(105)
@test lcm(a, b) === T(30)//T(7)
if T <: Signed
@test gcd(-a) === a
@test lcm(-b) === b
@test gcdx(a, b) === (T(2)//T(105), T(-11), T(4))
@test gcd(-a, b) === T(2)//T(105)
@test gcd(a, -b) === T(2)//T(105)
Expand Down

2 comments on commit fb42ea5

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your package evaluation job has completed - possible new issues were detected. A full report can be found here. cc @maleadt

Please sign in to comment.