-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Expand file tree
/
Copy pathdiv.jl
More file actions
480 lines (415 loc) · 16.7 KB
/
Copy pathdiv.jl
File metadata and controls
480 lines (415 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# This file is a part of Julia. License is MIT: https://julialang.org/license
# Div is truncating by default
"""
div(x, y, r::RoundingMode=RoundToZero)
The quotient from Euclidean (integer) division. Computes `x / y`, rounded to
an integer according to the rounding mode `r`. In other words, the quantity
round(x / y, r)
without any intermediate rounding.
!!! compat "Julia 1.4"
The three-argument method taking a `RoundingMode` requires Julia 1.4 or later.
See also [`fld`](@ref) and [`cld`](@ref), which are special cases of this function.
!!! compat "Julia 1.9"
`RoundFromZero` requires at least Julia 1.9.
# Examples:
```jldoctest
julia> div(4, 3, RoundToZero) # Matches div(4, 3)
1
julia> div(4, 3, RoundDown) # Matches fld(4, 3)
1
julia> div(4, 3, RoundUp) # Matches cld(4, 3)
2
julia> div(5, 2, RoundNearest)
2
julia> div(5, 2, RoundNearestTiesAway)
3
julia> div(-5, 2, RoundNearest)
-2
julia> div(-5, 2, RoundNearestTiesAway)
-3
julia> div(-5, 2, RoundNearestTiesUp)
-2
julia> div(4, 3, RoundFromZero)
2
julia> div(-4, 3, RoundFromZero)
-2
```
!!! note "Floating-point numbers"
Accurate results for floating-point arguments are only guaranteed when the
mathematical value ``\\frac{x}{y}`` is within the range of exactly representable
integers for the given floating-point type, that is, when `eps(x/y) ≤ 1`, or
in other words, given `a = div(x, y)`, when `abs(a) < maxintfloat(a)`.
Because `div(x, y)` implements strict truncated rounding based on the quotient
and remainder of the Euclidean division, and because the binary representation
of floating-point numbers (in most cases) only approximates the decimal
representation we use, unintuitive situations can arise. For example:
```jldoctest
julia> div(6.0, 0.1)
59.0
julia> 6.0 / 0.1
60.0
julia> 6.0 / big(0.1)
59.99999999999999666933092612453056361837965690217069245739573412231113406246995
```
What is happening here is that the binary representation of the `Float64`
number written as `0.1` is slightly larger than the numerical value ``0.1``
(just like `0.3333333333333333` is less than ``1/3`` in decimal), while `6.0`
represents the number ``6`` precisely. Therefore the mathematical result of
`6.0` divided by (the `Float64` representation of) `0.1` is slightly less
than ``60``. The result of the floating-point division is rounded to precisely
`60.0`, but `div(6.0, 0.1, RoundToZero)` takes account of the quotient and
(here non-zero) remainder of the Euclidean division, so the result is `59.0`.
See also [`rem`](@ref), [`divrem`](@ref).
"""
div(x, y, r::RoundingMode)
div(a, b) = div(a, b, RoundToZero)
"""
rem(x, y, r::RoundingMode=RoundToZero)
Compute the remainder of `x` after integer division by `y`, with the quotient rounded
according to the rounding mode `r`. In other words, the quantity
x - y * round(x / y, r)
without any intermediate rounding.
- if `r == RoundNearest`, then the result is exact, and in the interval
``[-|y| / 2, |y| / 2]``. See also [`RoundNearest`](@ref).
- if `r == RoundToZero` (default), then the result is exact, and in the interval
``[0, |y|)`` if `x` is positive, or ``(-|y|, 0]`` otherwise. See also [`RoundToZero`](@ref).
- if `r == RoundDown`, then the result is in the interval ``[0, |y|)`` if `y` is positive, or
``(-|y|, 0]`` otherwise. The result may not be exact if `x` and `y` have different signs, and
`abs(x) < abs(y)`. See also [`RoundDown`](@ref).
- if `r == RoundUp`, then the result is in the interval ``(-|y|, 0]`` if `y` is positive, or
``[0, |y|)`` otherwise. The result may not be exact if `x` and `y` have the same sign, and
`abs(x) < abs(y)`. See also [`RoundUp`](@ref).
- if `r == RoundFromZero`, then the result is in the interval ``(-|y|, 0]`` if `x` is positive, or
``[0, |y|)`` otherwise. The result may not be exact if `x` and `y` have the same sign, and
`abs(x) < abs(y)`. See also [`RoundFromZero`](@ref).
!!! compat "Julia 1.9"
`RoundFromZero` requires at least Julia 1.9.
# Examples:
```jldoctest
julia> x = 9; y = 4;
julia> x % y # same as rem(x, y)
1
julia> x ÷ y # same as div(x, y)
2
julia> x == div(x, y) * y + rem(x, y)
true
```
"""
rem(x, y, r::RoundingMode)
# TODO: Make these primitive and have the two-argument version call these
rem(x, y, ::RoundingMode{:ToZero}) = rem(x, y)
rem(x, y, ::RoundingMode{:Down}) = mod(x, y)
rem(x, y, ::RoundingMode{:Up}) = mod(x, -y)
rem(x, y, r::RoundingMode{:Nearest}) = x - y * div(x, y, r)
rem(x::Integer, y::Integer, r::RoundingMode{:Nearest}) = divrem(x, y, r)[2]
function rem(x::Integer, y::Integer, rnd::Union{typeof(RoundNearestTiesAway),
typeof(RoundNearestTiesUp)})
divrem(x, y, rnd)[2]
end
function rem(x, y, ::typeof(RoundFromZero))
signbit(x) == signbit(y) ? rem(x, y, RoundUp) : rem(x, y, RoundDown)
end
function rem(x::AbstractFloat, y::AbstractFloat, rnd::Union{typeof(RoundNearestTiesAway),
typeof(RoundNearestTiesUp)})
r = mod(x, y)
isnan(r) && return r
if !iszero(r)
m, n = abs(r) < floatmax(r)/2 ? abs.((2r, y)) : abs.((r, y/2))
m < n && return r
m > n && return mod(x, -y)
end
rnd === RoundNearestTiesUp || signbit(x) == signbit(y) ? mod(x, -y) : r
end
"""
fld(x, y)
Largest integer less than or equal to `x / y`. Equivalent to `div(x, y, RoundDown)`.
See also [`div`](@ref), [`cld`](@ref), [`mod`](@ref), [`fldmod`](@ref).
# Examples
```jldoctest
julia> fld(7.3, 5.5)
1.0
julia> fld.(-5:5, 3)'
1×11 adjoint(::Vector{Int64}) with eltype Int64:
-2 -2 -1 -1 -1 0 0 0 1 1 1
```
!!! note "Floating-point numbers"
Accurate results for floating-point arguments are only guaranteed when the
mathematical value ``\\frac{x}{y}`` is within the range of exactly representable
integers for the given floating-point type, that is, when `eps(x/y) ≤ 1`, or
in other words, given `a = fld(x, y)`, when `abs(a) < maxintfloat(a)`.
Because `fld(x, y)` implements strict floored rounding based on the quotient
and remainder of the Euclidean division, and because the binary representation
of floating-point numbers (in most cases) only approximates the decimal
representation we use, unintuitive situations can arise. For example:
```jldoctest
julia> fld(6.0, 0.1)
59.0
julia> 6.0 / 0.1
60.0
julia> 6.0 / big(0.1)
59.99999999999999666933092612453056361837965690217069245739573412231113406246995
```
What is happening here is that the binary representation of the `Float64`
number written as `0.1` is slightly larger than the numerical value ``0.1``
(just like `0.3333333333333333` is less than ``1/3`` in decimal), while `6.0`
represents the number ``6`` precisely. Therefore the mathematical result of
`6.0` divided by (the `Float64` representation of) `0.1` is slightly less
than ``60``. The result of the floating-point division is rounded to precisely
`60.0`, but `fld(6.0, 0.1)` takes account of the quotient and (here non-zero)
remainder of the Euclidean division, so the result is `59.0`.
See also [`rem`](@ref), [`divrem`](@ref).
"""
fld(a, b) = div(a, b, RoundDown)
"""
cld(x, y)
Smallest integer larger than or equal to `x / y`. Equivalent to `div(x, y, RoundUp)`.
See also [`div`](@ref), [`fld`](@ref), [`mod1`](@ref), [`cldmod1`](@ref).
# Examples
```jldoctest
julia> cld(5.5, 2.2)
3.0
julia> cld.(-5:5, 3)'
1×11 adjoint(::Vector{Int64}) with eltype Int64:
-1 -1 -1 0 0 0 1 1 1 2 2
```
!!! note "Floating-point numbers"
Accurate results for floating-point arguments are only guaranteed when the
mathematical value ``\\frac{x}{y}`` is within the range of exactly representable
integers for the given floating-point type, that is, when `eps(x/y) ≤ 1`, or
in other words, given `a = cld(x, y)`, when `abs(a) < maxintfloat(a)`.
Because `cld(x, y)` implements strict ceiled rounding based on the quotient
and remainder of the Euclidean division, and because the binary representation
of floating-point numbers (in most cases) only approximates the decimal
representation we use, unintuitive situations can arise. For example:
```jldoctest
julia> cld(3.0, 0.3)
11.0
julia> 3.0 / 0.3
10.0
julia> 3.0 / big(0.3)
10.00000000000000037007434154171886050337904945061778828900298697586147515340753
```
What is happening here is that the binary representation of the `Float64`
number written as `0.3` is slightly less than the numerical value ``0.3``
(just like `0.3333333333333333` is less than ``1/3`` in decimal), while `3.0`
represents the number ``3`` precisely. Therefore the mathematical result of
`3.0` divided by (the `Float64` representation of) `0.3` is slightly larger
than ``10``. The result of the floating-point division is rounded to precisely
`10.0`, but `cld(3.0, 0.3)` takes account of the quotient and (here non-zero)
remainder of the Euclidean division, so the result is `11.0`.
See also [`rem`](@ref), [`divrem`](@ref).
"""
cld(a, b) = div(a, b, RoundUp)
"""
fld1(a, b)
Legacy spelling of `cld(a, b)` for integers.
See also [`cld`](@ref).
"""
fld1(a, b) = cld(a, b)
# divrem
"""
divrem(x, y, r::RoundingMode=RoundToZero)
The quotient and remainder from Euclidean division.
Equivalent to `(div(x, y, r), rem(x, y, r))`. Equivalently, with the default
value of `r`, this call is equivalent to `(x ÷ y, x % y)`.
See also [`fldmod`](@ref), [`cldmod1`](@ref), [`div`](@ref), [`rem`](@ref).
# Examples
```jldoctest
julia> divrem(3, 7)
(0, 3)
julia> divrem(7, 3)
(2, 1)
```
"""
divrem(x, y) = divrem(x, y, RoundToZero)
function divrem(a, b, r::RoundingMode)
if r === RoundToZero
# For compat. Remove in 2.0.
(div(a, b), rem(a, b))
elseif r === RoundDown
# For compat. Remove in 2.0.
(fld(a, b), mod(a, b))
else
(div(a, b, r), rem(a, b, r))
end
end
# avoids calling rem for Integers-Integers (all modes),
# a - d * b not precise for Floats - AbstractFloat, AbstractIrrational.
# Rationals are still slower
function divrem(a::Integer, b::Integer, r::Union{typeof(RoundUp),
typeof(RoundDown),
typeof(RoundToZero)})
if r === RoundToZero
# For compat. Remove in 2.0.
d = div(a, b)
(d, a - d * b)
elseif r === RoundDown
# For compat. Remove in 2.0.
d = fld(a, b)
(d, a - d * b)
elseif r === RoundUp
# For compat. Remove in 2.0.
d = div(a, b, r)
(d, a - d * b)
end
end
# For fixed-width integers the floored/ceiled quotient times `b` lies in
# `(a - b, a + b]` and so wraps on valid inputs (e.g. `fld(typemin(Int), 3)*3`);
# the remainder is correct only through wrap-cancellation. The truncated
# quotient satisfies `|d*b| <= |a|`, so the RoundToZero branch cannot overflow
# and stays on checkable operators.
function divrem(a::T, b::T, r::Union{typeof(RoundUp),
typeof(RoundDown),
typeof(RoundToZero)}) where T<:BitSigned
if r === RoundToZero
d = div(a, b)
(d, a - d * b)
elseif r === RoundDown
d = fld(a, b)
(d, a -% d *% b)
elseif r === RoundUp
d = div(a, b, r)
(d, a -% d *% b)
end
end
function divrem(x::Integer, y::Integer, rnd::typeof(RoundNearest))
(q, r) = divrem(x, y)
if x >= 0
if y >= 0
r >= (y÷2) + (isodd(y) | iseven(q)) ? (q+true, r-y) : (q, r)
else
r >= -(y÷2) + (isodd(y) | iseven(q)) ? (q-true, r+y) : (q, r)
end
else
if y >= 0
r <= -signed(y÷2) - (isodd(y) | iseven(q)) ? (q-true, r+y) : (q, r)
else
r <= (y÷2) - (isodd(y) | iseven(q)) ? (q+true, r-y) : (q, r)
end
end
end
function divrem(x::Integer, y::Integer, rnd:: typeof(RoundNearestTiesAway))
(q, r) = divrem(x, y)
if x >= 0
if y >= 0
r >= (y÷2) + isodd(y) ? (q+true, r-y) : (q, r)
else
r >= -(y÷2) + isodd(y) ? (q-true, r+y) : (q, r)
end
else
if y >= 0
r <= -signed(y÷2) - isodd(y) ? (q-true, r+y) : (q, r)
else
r <= (y÷2) - isodd(y) ? (q+true, r-y) : (q, r)
end
end
end
function divrem(x::Integer, y::Integer, rnd::typeof(RoundNearestTiesUp))
(q, r) = divrem(x, y)
if x >= 0
if y >= 0
r >= (y÷2) + isodd(y) ? (q+true, r-y) : (q, r)
else
r >= -(y÷2) + true ? (q-true, r+y) : (q, r)
end
else
if y >= 0
r <= -signed(y÷2) - true ? (q-true, r+y) : (q, r)
else
r <= (y÷2) - isodd(y) ? (q+true, r-y) : (q, r)
end
end
end
function divrem(x, y, ::typeof(RoundFromZero))
signbit(x) == signbit(y) ? divrem(x, y, RoundUp) : divrem(x, y, RoundDown)
end
"""
fldmod(x, y)
The floored quotient and modulus after division. A convenience wrapper for
`divrem(x, y, RoundDown)`. Equivalent to `(fld(x, y), mod(x, y))`.
See also [`fld`](@ref), [`mod`](@ref), [`divrem`](@ref), [`cldmod1`](@ref).
"""
fldmod(x, y) = divrem(x, y, RoundDown)
# We define generic rounding methods for other rounding modes in terms of
# RoundToZero.
function div(x::Signed, y::Unsigned, ::typeof(RoundDown))
(q, r) = divrem(x, y)
q - (signbit(x) & (r != 0))
end
function div(x::Unsigned, y::Signed, ::typeof(RoundDown))
(q, r) = divrem(x, y)
q - (signbit(y) & (r != 0))
end
function div(x::Signed, y::Unsigned, ::typeof(RoundUp))
(q, r) = divrem(x, y)
q + (!signbit(x) & (r != 0))
end
function div(x::Unsigned, y::Signed, ::typeof(RoundUp))
(q, r) = divrem(x, y)
q + (!signbit(y) & (r != 0))
end
function div(x::Integer, y::Integer, rnd::Union{typeof(RoundNearest),
typeof(RoundNearestTiesAway),
typeof(RoundNearestTiesUp)})
divrem(x, y, rnd)[1]
end
function div(x::Integer, y::Integer, ::typeof(RoundFromZero))
signbit(x) == signbit(y) ? div(x, y, RoundUp) : div(x, y, RoundDown)
end
# For bootstrapping purposes, we define div for integers directly. Provide the
# generic signature also
div(a::T, b::T, ::typeof(RoundToZero)) where {T<:Union{BitSigned, BitUnsigned}} = div(a, b)
div(a::Bool, b::Bool, r::RoundingMode) = div(a, b)
# Prevent ambiguities
for rm in (RoundUp, RoundDown, RoundToZero, RoundFromZero)
@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)
# 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
# be removed.
fld(x::Real, y::Real) = div(promote(x, y)..., RoundDown)
cld(x::Real, y::Real) = div(promote(x, y)..., RoundUp)
fld(x::Signed, y::Unsigned) = div(x, y, RoundDown)
fld(x::Unsigned, y::Signed) = div(x, y, RoundDown)
cld(x::Signed, y::Unsigned) = div(x, y, RoundUp)
cld(x::Unsigned, y::Signed) = div(x, y, RoundUp)
fld(x::T, y::T) where {T<:Real} = throw(MethodError(div, (x, y, RoundDown)))
cld(x::T, y::T) where {T<:Real} = throw(MethodError(div, (x, y, RoundUp)))
# Promotion
function div(x::Real, y::Real, r::RoundingMode)
typeof(x) === typeof(y) && throw(MethodError(div, (x, y, r)))
if r === RoundToZero
# For compat. Remove in 2.0.
div(promote(x, y)...)
else
div(promote(x, y)..., r)
end
end
# Integers
# fld(x, y) == div(x, y) - ((x >= 0) != (y >= 0) && rem(x, y) != 0 ? 1 : 0)
div(x::T, y::T, ::typeof(RoundDown)) where {T<:Unsigned} = div(x, y)
function div(x::T, y::T, ::typeof(RoundDown)) where T<:Integer
d = div(x, y, RoundToZero)
return d - (signbit(x ⊻ y) & (d * y != x))
end
# cld(x, y) = div(x, y) + ((x > 0) == (y > 0) && rem(x, y) != 0 ? 1 : 0)
function div(x::T, y::T, ::typeof(RoundUp)) where T<:Unsigned
d = div(x, y, RoundToZero)
return d + (d * y != x)
end
function div(x::T, y::T, ::typeof(RoundUp)) where T<:Integer
d = div(x, y, RoundToZero)
return d + (((x > 0) == (y > 0)) & (d * y != x))
end
# Floats
# NB. If eps(x/y) > 1, x/y rounds to an unsafe integer which can't be floored
# or ceiled if it needs to since x/y ± 1 is not representable.
# @see https://github.com/JuliaLang/julia/issues/49450#issuecomment-3694946121
div(x::T, y::T, r::RoundingMode) where {T<:AbstractFloat} = round(x / y - rem(x, y, r) / y)