From bcb5605d0ce85586ff584989153181f4566467f7 Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Wed, 25 Sep 2019 18:38:12 -0400 Subject: [PATCH] Bugfixes --- base/div.jl | 13 +++++++++++++ base/int.jl | 11 +++++++++++ 2 files changed, 24 insertions(+) diff --git a/base/div.jl b/base/div.jl index 6e8d9063b7ea0..74d2b618ceaca 100644 --- a/base/div.jl +++ b/base/div.jl @@ -202,8 +202,21 @@ end # generic signature also div(a::T, b::T, ::typeof(RoundToZero)) where {T<:Union{BitSigned, BitUnsigned64}} = div(a, b) div(a::Bool, b::Bool, r::RoundingMode) = div(a, b) +# Prevent ambiguities +for rm in (RoundUp, RoundDown, RoundToZero) + @eval div(a::Bool, b::Bool, r::$(typeof(rm))) = div(a, b) +end +function div(x::Bool, y::Bool, rnd::Union{typeof(RoundNearest), + typeof(RoundNearestTiesAway), + typeof(RoundNearestTiesUp)}) + div(x, y) +end fld(a::T, b::T) where {T<:Union{Integer,AbstractFloat}} = div(a, b, RoundDown) cld(a::T, b::T) where {T<:Union{Integer,AbstractFloat}} = div(a, b, RoundUp) +div(a::Int128, b::Int128, ::typeof(RoundToZero)) = div(a, b) +div(a::UInt128, b::UInt128, ::typeof(RoundToZero)) = div(a, b) +rem(a::Int128, b::Int128, ::typeof(RoundToZero)) = rem(a, b) +rem(a::UInt128, b::UInt128, ::typeof(RoundToZero)) = rem(a, b) # These are kept for compatibility with external packages overriding fld/cld. # In 2.0, packages should extend div(a,b,r) instead, in which case, these can diff --git a/base/int.jl b/base/int.jl index 50b29001ff90b..beef3b74c3d48 100644 --- a/base/int.jl +++ b/base/int.jl @@ -175,6 +175,17 @@ div(x::Unsigned, y::BitSigned) = unsigned(flipsign(signed(div(x, unsigned(abs(y) rem(x::BitSigned, y::Unsigned) = flipsign(signed(rem(unsigned(abs(x)), y)), x) rem(x::Unsigned, y::BitSigned) = rem(x, unsigned(abs(y))) +function divrem(x::BitSigned, y::Unsigned) + q, r = divrem(unsigned(abs(x)), y) + flipsign(signed(q), x), flipsign(signed(r), x) +end + +function divrem(x::Unsigned, y::BitSigned) + q, r = divrem(x, unsigned(abs(y))) + unsigned(flipsign(signed(q), y)), r +end + + """ mod(x, y) rem(x, y, RoundDown)