@@ -17,6 +17,9 @@ without any intermediate rounding.
1717
1818See also [`fld`](@ref) and [`cld`](@ref), which are special cases of this function.
1919
20+ !!! compat "Julia 1.9"
21+ `RoundFromZero` requires at least Julia 1.9.
22+
2023# Examples:
2124```jldoctest
2225julia> div(4, 3, RoundDown) # Matches fld(4, 3)
@@ -33,6 +36,10 @@ julia> div(-5, 2, RoundNearestTiesAway)
3336-3
3437julia> div(-5, 2, RoundNearestTiesUp)
3538-2
39+ julia> div(4, 3, RoundFromZero)
40+ 2
41+ julia> div(-4, 3, RoundFromZero)
42+ -2
3643```
3744"""
3845div (x, y, r:: RoundingMode )
@@ -63,6 +70,13 @@ without any intermediate rounding.
6370 `[0,-y)` otherwise. The result may not be exact if `x` and `y` have the same sign, and
6471 `abs(x) < abs(y)`. See also [`RoundUp`](@ref).
6572
73+ - if `r == RoundFromZero`, then the result is in the interval `(-y, 0]` if `y` is positive, or
74+ `[0, -y)` otherwise. The result may not be exact if `x` and `y` have the same sign, and
75+ `abs(x) < abs(y)`. See also [`RoundFromZero`](@ref).
76+
77+ !!! compat "Julia 1.9"
78+ `RoundFromZero` requires at least Julia 1.9.
79+
6680# Examples:
6781```jldoctest
6882julia> x = 9; y = 4;
@@ -86,6 +100,10 @@ rem(x, y, ::RoundingMode{:Up}) = mod(x, -y)
86100rem (x, y, r:: RoundingMode{:Nearest} ) = x - y* div (x, y, r)
87101rem (x:: Integer , y:: Integer , r:: RoundingMode{:Nearest} ) = divrem (x, y, r)[2 ]
88102
103+ function rem (x, y, :: typeof (RoundFromZero))
104+ signbit (x) == signbit (y) ? rem (x, y, RoundUp) : rem (x, y, RoundDown)
105+ end
106+
89107"""
90108 fld(x, y)
91109
@@ -240,6 +258,10 @@ function divrem(x::Integer, y::Integer, rnd::typeof(RoundNearestTiesUp))
240258 end
241259end
242260
261+ function divrem (x, y, :: typeof (RoundFromZero))
262+ signbit (x) == signbit (y) ? divrem (x, y, RoundUp) : divrem (x, y, RoundDown)
263+ end
264+
243265"""
244266 fldmod(x, y)
245267
@@ -276,12 +298,16 @@ function div(x::Integer, y::Integer, rnd::Union{typeof(RoundNearest),
276298 divrem (x, y, rnd)[1 ]
277299end
278300
301+ function div (x:: Integer , y:: Integer , :: typeof (RoundFromZero))
302+ signbit (x) == signbit (y) ? div (x, y, RoundUp) : div (x, y, RoundDown)
303+ end
304+
279305# For bootstrapping purposes, we define div for integers directly. Provide the
280306# generic signature also
281307div (a:: T , b:: T , :: typeof (RoundToZero)) where {T<: Union{BitSigned, BitUnsigned64} } = div (a, b)
282308div (a:: Bool , b:: Bool , r:: RoundingMode ) = div (a, b)
283309# Prevent ambiguities
284- for rm in (RoundUp, RoundDown, RoundToZero)
310+ for rm in (RoundUp, RoundDown, RoundToZero, RoundFromZero )
285311 @eval div (a:: Bool , b:: Bool , r:: $ (typeof (rm))) = div (a, b)
286312end
287313function div (x:: Bool , y:: Bool , rnd:: Union {typeof (RoundNearest),
0 commit comments