Skip to content

Commit

Permalink
Fix array-argument gcd for non-integers
Browse files Browse the repository at this point in the history
  • Loading branch information
sostock committed Jan 17, 2020
1 parent 4c58369 commit 3549cd1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 2 additions & 1 deletion base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ lcm(a::Union{Integer,Rational}, b::Union{Integer,Rational}) = lcm(promote(a,b)..
gcd(a::Union{Integer,Rational}, b::Union{Integer,Rational}...) = gcd(a, gcd(b...))
lcm(a::Union{Integer,Rational}, b::Union{Integer,Rational}...) = lcm(a, lcm(b...))

gcd(abc::AbstractArray{<:Union{Integer,Rational}}) = reduce(gcd, abc; init=zero(eltype(abc)))
lcm(abc::AbstractArray{<:Union{Integer,Rational}}) = reduce(lcm, abc; init=one(eltype(abc)))

function gcd(abc::AbstractArray{<:Union{Integer,Rational}})
function gcd(abc::AbstractArray{<:Integer})
a = zero(eltype(abc))
for b in abc
a = gcd(a,b)
Expand Down
2 changes: 2 additions & 0 deletions test/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -399,5 +399,7 @@ end
@test lcm(1//3, 1) == 1//1
@test lcm(3//1, 1//0) == 3//1
@test lcm(0//1, 1//0) == 0//1

@test gcd([5, 2, 1//2]) == 1//2
end

0 comments on commit 3549cd1

Please sign in to comment.