Skip to content

Commit

Permalink
Make lcm(Rational[]) throw.
Browse files Browse the repository at this point in the history
  • Loading branch information
LilithHafner authored Dec 31, 2024
1 parent 79d3f77 commit 2ef55d8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ gcd(abc::AbstractArray{<:Real}) = reduce(gcd, abc; init=zero(eltype(abc)))
function lcm(abc::AbstractArray{<:Real})
# Using reduce with init=one(eltype(abc)) is buggy for Rationals.
l = length(abc)
l == 0 && return one(eltype(abc))
if l == 0
eltype(abc) <: Integer && return one(eltype(abc))
throw(ArgumentError("lcm has no identity over $(eltype(abc))"))
end
l == 1 && return abs(only(abc))
return reduce(lcm, abc)
end
Expand Down
2 changes: 1 addition & 1 deletion test/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ end
@test gcd(Int[]) === 0
@test lcm(Int[]) === 1
@test gcd(Rational{Int}[]) === 0//1
@test lcm(Rational{Int}[]) === 1//1
@test_throws ArgumentError("lcm has no identity on Rational{Int}") lcm(Rational{Int}[])
end

(a::Tuple{T, T, T}, b::Tuple{T, T, T}) where T <: Union{Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128} = a === b
Expand Down

0 comments on commit 2ef55d8

Please sign in to comment.