From 3549cd190ecd0574c803de80e5f6d8d70cbf05e3 Mon Sep 17 00:00:00 2001 From: Sebastian Stock <42280794+sostock@users.noreply.github.com> Date: Fri, 17 Jan 2020 11:07:05 +0100 Subject: [PATCH] Fix array-argument gcd for non-integers --- base/intfuncs.jl | 3 ++- test/rational.jl | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/base/intfuncs.jl b/base/intfuncs.jl index 77938f58d5b33..2e1ec8fc50a1b 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -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) diff --git a/test/rational.jl b/test/rational.jl index 083fb1c19c10f..52f502b4178a6 100644 --- a/test/rational.jl +++ b/test/rational.jl @@ -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