• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #38182

15 Aug 2025 03:55AM UTC coverage: 77.87% (-0.4%) from 78.28%
#38182

push

local

web-flow
🤖 [master] Bump the SparseArrays stdlib from 30201ab to bb5ecc0 (#59263)

Stdlib: SparseArrays
URL: https://github.com/JuliaSparse/SparseArrays.jl.git
Stdlib branch: main
Julia branch: master
Old commit: 30201ab
New commit: bb5ecc0
Julia version: 1.13.0-DEV
SparseArrays version: 1.13.0
Bump invoked by: @ViralBShah
Powered by:
[BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl)

Diff:
https://github.com/JuliaSparse/SparseArrays.jl/compare/30201abcb...bb5ecc091

```
$ git log --oneline 30201ab..bb5ecc0
bb5ecc0 fast quadratic form for dense matrix, sparse vectors (#640)
34ece87 Extend 3-arg `dot` to generic `HermOrSym` sparse matrices (#643)
095b685 Exclude unintended complex symmetric sparse matrices from 3-arg `dot` (#642)
8049287 Fix signature for 2-arg matrix-matrix `dot` (#641)
cff971d Make cond(::SparseMatrix, 1 / Inf) discoverable from 2-norm error (#629)
```

Co-authored-by: ViralBShah <744411+ViralBShah@users.noreply.github.com>

48274 of 61993 relevant lines covered (77.87%)

9571166.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.31
/base/div.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
# Div is truncating by default
4

5
"""
6
    div(x, y, r::RoundingMode=RoundToZero)
7

8
The quotient from Euclidean (integer) division. Computes `x / y`, rounded to
9
an integer according to the rounding mode `r`. In other words, the quantity
10

11
    round(x / y, r)
12

13
without any intermediate rounding.
14

15
!!! compat "Julia 1.4"
16
    The three-argument method taking a `RoundingMode` requires Julia 1.4 or later.
17

18
See also [`fld`](@ref) and [`cld`](@ref), which are special cases of this function.
19

20
!!! compat "Julia 1.9"
21
    `RoundFromZero` requires at least Julia 1.9.
22

23
# Examples:
24
```jldoctest
25
julia> div(4, 3, RoundToZero) # Matches div(4, 3)
26
1
27
julia> div(4, 3, RoundDown) # Matches fld(4, 3)
28
1
29
julia> div(4, 3, RoundUp) # Matches cld(4, 3)
30
2
31
julia> div(5, 2, RoundNearest)
32
2
33
julia> div(5, 2, RoundNearestTiesAway)
34
3
35
julia> div(-5, 2, RoundNearest)
36
-2
37
julia> div(-5, 2, RoundNearestTiesAway)
38
-3
39
julia> div(-5, 2, RoundNearestTiesUp)
40
-2
41
julia> div(4, 3, RoundFromZero)
42
2
43
julia> div(-4, 3, RoundFromZero)
44
-2
45
```
46
Because `div(x, y)` implements strictly correct truncated rounding based on the true
47
value of floating-point numbers, unintuitive situations can arise. For example:
48
```jldoctest
49
julia> div(6.0, 0.1)
50
59.0
51
julia> 6.0 / 0.1
52
60.0
53
julia> 6.0 / big(0.1)
54
59.99999999999999666933092612453056361837965690217069245739573412231113406246995
55
```
56
What is happening here is that the true value of the floating-point number written
57
as `0.1` is slightly larger than the numerical value 1/10 while `6.0` represents
58
the number 6 precisely. Therefore the true value of `6.0 / 0.1` is slightly less
59
than 60. When doing division, this is rounded to precisely `60.0`, but
60
`div(6.0, 0.1, RoundToZero)` always truncates the true value, so the result is `59.0`.
61
"""
62
div(x, y, r::RoundingMode)
63

64
div(a, b) = div(a, b, RoundToZero)
227,626,460✔
65

66
"""
67
    rem(x, y, r::RoundingMode=RoundToZero)
68

69
Compute the remainder of `x` after integer division by `y`, with the quotient rounded
70
according to the rounding mode `r`. In other words, the quantity
71

72
    x - y * round(x / y, r)
73

74
without any intermediate rounding.
75

76
- if `r == RoundNearest`, then the result is exact, and in the interval
77
  ``[-|y| / 2, |y| / 2]``. See also [`RoundNearest`](@ref).
78

79
- if `r == RoundToZero` (default), then the result is exact, and in the interval
80
  ``[0, |y|)`` if `x` is positive, or ``(-|y|, 0]`` otherwise. See also [`RoundToZero`](@ref).
81

82
- if `r == RoundDown`, then the result is in the interval ``[0, y)`` if `y` is positive, or
83
  ``(y, 0]`` otherwise. The result may not be exact if `x` and `y` have different signs, and
84
  `abs(x) < abs(y)`. See also [`RoundDown`](@ref).
85

86
- if `r == RoundUp`, then the result is in the interval ``(-y, 0]`` if `y` is positive, or
87
  ``[0, -y)`` otherwise. The result may not be exact if `x` and `y` have the same sign, and
88
  `abs(x) < abs(y)`. See also [`RoundUp`](@ref).
89

90
- if `r == RoundFromZero`, then the result is in the interval ``(-y, 0]`` if `y` is positive, or
91
  ``[0, -y)`` otherwise. The result may not be exact if `x` and `y` have the same sign, and
92
  `abs(x) < abs(y)`. See also [`RoundFromZero`](@ref).
93

94
!!! compat "Julia 1.9"
95
    `RoundFromZero` requires at least Julia 1.9.
96

97
# Examples:
98
```jldoctest
99
julia> x = 9; y = 4;
100

101
julia> x % y  # same as rem(x, y)
102
1
103

104
julia> x ÷ y  # same as div(x, y)
105
2
106

107
julia> x == div(x, y) * y + rem(x, y)
108
true
109
```
110
"""
111
rem(x, y, r::RoundingMode)
112

113
# TODO: Make these primitive and have the two-argument version call these
114
rem(x, y, ::RoundingMode{:ToZero}) = rem(x, y)
538,179✔
115
rem(x, y, ::RoundingMode{:Down}) = mod(x, y)
58,821✔
116
rem(x, y, ::RoundingMode{:Up}) = mod(x, -y)
36,334✔
117
rem(x, y, r::RoundingMode{:Nearest}) = x - y * div(x, y, r)
100✔
118
rem(x::Integer, y::Integer, r::RoundingMode{:Nearest}) = divrem(x, y, r)[2]
214✔
119

120
function rem(x, y, ::typeof(RoundFromZero))
36✔
121
    signbit(x) == signbit(y) ? rem(x, y, RoundUp) : rem(x, y, RoundDown)
36✔
122
end
123

124
"""
125
    fld(x, y)
126

127
Largest integer less than or equal to `x / y`. Equivalent to `div(x, y, RoundDown)`.
128

129
See also [`div`](@ref), [`cld`](@ref), [`fld1`](@ref).
130

131
# Examples
132
```jldoctest
133
julia> fld(7.3, 5.5)
134
1.0
135

136
julia> fld.(-5:5, 3)'
137
1×11 adjoint(::Vector{Int64}) with eltype Int64:
138
 -2  -2  -1  -1  -1  0  0  0  1  1  1
139
```
140
Because `fld(x, y)` implements strictly correct floored rounding based on the true
141
value of floating-point numbers, unintuitive situations can arise. For example:
142
```jldoctest
143
julia> fld(6.0, 0.1)
144
59.0
145
julia> 6.0 / 0.1
146
60.0
147
julia> 6.0 / big(0.1)
148
59.99999999999999666933092612453056361837965690217069245739573412231113406246995
149
```
150
What is happening here is that the true value of the floating-point number written
151
as `0.1` is slightly larger than the numerical value 1/10 while `6.0` represents
152
the number 6 precisely. Therefore the true value of `6.0 / 0.1` is slightly less
153
than 60. When doing division, this is rounded to precisely `60.0`, but
154
`fld(6.0, 0.1)` always takes the floor of the true value, so the result is `59.0`.
155
"""
156
fld(a, b) = div(a, b, RoundDown)
4✔
157

158
"""
159
    cld(x, y)
160

161
Smallest integer larger than or equal to `x / y`. Equivalent to `div(x, y, RoundUp)`.
162

163
See also [`div`](@ref), [`fld`](@ref).
164

165
# Examples
166
```jldoctest
167
julia> cld(5.5, 2.2)
168
3.0
169

170
julia> cld.(-5:5, 3)'
171
1×11 adjoint(::Vector{Int64}) with eltype Int64:
172
 -1  -1  -1  0  0  0  1  1  1  2  2
173
```
174
"""
175
cld(a, b) = div(a, b, RoundUp)
×
176

177
# divrem
178
"""
179
    divrem(x, y, r::RoundingMode=RoundToZero)
180

181
The quotient and remainder from Euclidean division.
182
Equivalent to `(div(x, y, r), rem(x, y, r))`. Equivalently, with the default
183
value of `r`, this call is equivalent to `(x ÷ y, x % y)`.
184

185
See also: [`fldmod`](@ref), [`cld`](@ref).
186

187
# Examples
188
```jldoctest
189
julia> divrem(3, 7)
190
(0, 3)
191

192
julia> divrem(7, 3)
193
(2, 1)
194
```
195
"""
196
divrem(x, y) = divrem(x, y, RoundToZero)
232,767,945✔
197

198

199
function divrem(a, b, r::RoundingMode)
27,202✔
200
    if r === RoundToZero
289,357✔
201
        # For compat. Remove in 2.0.
202
        (div(a, b), rem(a, b))
277,435✔
203
    elseif r === RoundDown
15,204✔
204
        # For compat. Remove in 2.0.
205
        (fld(a, b), mod(a, b))
15,188✔
206
    else
207
        (div(a, b, r), rem(a, b, r))
16✔
208
    end
209
end
210
# avoids calling rem for Integers-Integers (all modes),
211
# a - d * b not precise for Floats - AbstractFloat, AbstractIrrational.
212
# Rationals are still slower
213
function divrem(a::Integer, b::Integer, r::Union{typeof(RoundUp),
31✔
214
                                                typeof(RoundDown),
215
                                                typeof(RoundToZero)})
216
    if r === RoundToZero
231,737,985✔
217
        # For compat. Remove in 2.0.
218
        d = div(a, b)
232,499,072✔
219
        (d, a - d * b)
232,200,910✔
220
    elseif r === RoundDown
484✔
221
        # For compat. Remove in 2.0.
222
        d = fld(a, b)
484✔
223
        (d, a - d * b)
480✔
224
    elseif r === RoundUp
×
225
        # For compat. Remove in 2.0.
226
        d = div(a, b, r)
×
227
        (d, a - d * b)
×
228
    end
229
end
230
function divrem(x::Integer, y::Integer, rnd::typeof(RoundNearest))
800✔
231
    (q, r) = divrem(x, y)
802✔
232
    if x >= 0
792✔
233
        if y >= 0
538✔
234
            r >=        (y÷2) + (isodd(y) | iseven(q)) ? (q+true, r-y) : (q, r)
451✔
235
        else
236
            r >=       -(y÷2) + (isodd(y) | iseven(q)) ? (q-true, r+y) : (q, r)
87✔
237
        end
238
    else
239
        if y >= 0
254✔
240
            r <= -signed(y÷2) - (isodd(y) | iseven(q)) ? (q-true, r+y) : (q, r)
170✔
241
        else
242
            r <=        (y÷2) - (isodd(y) | iseven(q)) ? (q+true, r-y) : (q, r)
84✔
243
        end
244
    end
245
end
246
function divrem(x::Integer, y::Integer, rnd:: typeof(RoundNearestTiesAway))
88✔
247
    (q, r) = divrem(x, y)
88✔
248
    if x >= 0
87✔
249
        if y >= 0
48✔
250
            r >=        (y÷2) + isodd(y) ? (q+true, r-y) : (q, r)
48✔
251
        else
252
            r >=       -(y÷2) + isodd(y) ? (q-true, r+y) : (q, r)
×
253
        end
254
    else
255
        if y >= 0
39✔
256
            r <= -signed(y÷2) - isodd(y) ? (q-true, r+y) : (q, r)
39✔
257
        else
258
            r <=        (y÷2) - isodd(y) ? (q+true, r-y) : (q, r)
×
259
        end
260
    end
261
end
262
function divrem(x::Integer, y::Integer, rnd::typeof(RoundNearestTiesUp))
89✔
263
    (q, r) = divrem(x, y)
89✔
264
    if x >= 0
88✔
265
        if y >= 0
49✔
266
            r >=        (y÷2) + isodd(y) ? (q+true, r-y) : (q, r)
49✔
267
        else
268
            r >=       -(y÷2) + true     ? (q-true, r+y) : (q, r)
×
269
        end
270
    else
271
        if y >= 0
39✔
272
            r <= -signed(y÷2) - true     ? (q-true, r+y) : (q, r)
39✔
273
        else
274
            r <=        (y÷2) - isodd(y) ? (q+true, r-y) : (q, r)
×
275
        end
276
    end
277
end
278

279
function divrem(x, y, ::typeof(RoundFromZero))
×
280
    signbit(x) == signbit(y) ? divrem(x, y, RoundUp) : divrem(x, y, RoundDown)
×
281
end
282

283
"""
284
    fldmod(x, y)
285

286
The floored quotient and modulus after division. A convenience wrapper for
287
`divrem(x, y, RoundDown)`. Equivalent to `(fld(x, y), mod(x, y))`.
288

289
See also: [`fld`](@ref), [`cld`](@ref), [`fldmod1`](@ref).
290
"""
291
fldmod(x, y) = divrem(x, y, RoundDown)
15,663✔
292

293
# We definite generic rounding methods for other rounding modes in terms of
294
# RoundToZero.
295
function div(x::Signed, y::Unsigned, ::typeof(RoundDown))
20✔
296
    (q, r) = divrem(x, y)
139✔
297
    q - (signbit(x) & (r != 0))
139✔
298
end
299
function div(x::Unsigned, y::Signed, ::typeof(RoundDown))
×
300
    (q, r) = divrem(x, y)
311✔
301
    q - (signbit(y) & (r != 0))
311✔
302
end
303

304
function div(x::Signed, y::Unsigned, ::typeof(RoundUp))
20✔
305
    (q, r) = divrem(x, y)
139✔
306
    q + (!signbit(x) & (r != 0))
139✔
307
end
308
function div(x::Unsigned, y::Signed, ::typeof(RoundUp))
×
309
    (q, r) = divrem(x, y)
273✔
310
    q + (!signbit(y) & (r != 0))
273✔
311
end
312

313
function div(x::Integer, y::Integer, rnd::Union{typeof(RoundNearest),
14✔
314
                                              typeof(RoundNearestTiesAway),
315
                                              typeof(RoundNearestTiesUp)})
316
    divrem(x, y, rnd)[1]
751✔
317
end
318

319
function div(x::Integer, y::Integer, ::typeof(RoundFromZero))
×
320
    signbit(x) == signbit(y) ? div(x, y, RoundUp) : div(x, y, RoundDown)
×
321
end
322

323
# For bootstrapping purposes, we define div for integers directly. Provide the
324
# generic signature also
325
div(a::T, b::T, ::typeof(RoundToZero)) where {T<:Union{BitSigned, BitUnsigned64}} = div(a, b)
59,249,238✔
326
div(a::Bool, b::Bool, r::RoundingMode) = div(a, b)
×
327
# Prevent ambiguities
328
for rm in (RoundUp, RoundDown, RoundToZero, RoundFromZero)
329
    @eval div(a::Bool, b::Bool, r::$(typeof(rm))) = div(a, b)
6✔
330
end
331
function div(x::Bool, y::Bool, rnd::Union{typeof(RoundNearest),
×
332
                                        typeof(RoundNearestTiesAway),
333
                                        typeof(RoundNearestTiesUp)})
334
    div(x, y)
×
335
end
336
fld(a::T, b::T) where {T<:Union{Integer,AbstractFloat}} = div(a, b, RoundDown)
50,771,221✔
337
cld(a::T, b::T) where {T<:Union{Integer,AbstractFloat}} = div(a, b, RoundUp)
1,111,528✔
338
div(a::Int128, b::Int128, ::typeof(RoundToZero)) = div(a, b)
1,009,070✔
339
div(a::UInt128, b::UInt128, ::typeof(RoundToZero)) = div(a, b)
98✔
340
rem(a::Int128, b::Int128, ::typeof(RoundToZero)) = rem(a, b)
×
341
rem(a::UInt128, b::UInt128, ::typeof(RoundToZero)) = rem(a, b)
×
342

343
# These are kept for compatibility with external packages overriding fld / cld.
344
# In 2.0, packages should extend div(a, b, r) instead, in which case, these can
345
# be removed.
346
fld(x::Real, y::Real) = div(promote(x, y)..., RoundDown)
28,316✔
347
cld(x::Real, y::Real) = div(promote(x, y)..., RoundUp)
13,002✔
348
fld(x::Signed, y::Unsigned) = div(x, y, RoundDown)
139✔
349
fld(x::Unsigned, y::Signed) = div(x, y, RoundDown)
311✔
350
cld(x::Signed, y::Unsigned) = div(x, y, RoundUp)
139✔
351
cld(x::Unsigned, y::Signed) = div(x, y, RoundUp)
273✔
352
fld(x::T, y::T) where {T<:Real} = throw(MethodError(div, (x, y, RoundDown)))
1✔
353
cld(x::T, y::T) where {T<:Real} = throw(MethodError(div, (x, y, RoundUp)))
1✔
354

355
# Promotion
356
function div(x::Real, y::Real, r::RoundingMode)
18✔
357
    typeof(x) === typeof(y) && throw(MethodError(div, (x, y, r)))
226,338,243✔
358
    if r === RoundToZero
226,337,052✔
359
        # For compat. Remove in 2.0.
360
        div(promote(x, y)...)
226,892,136✔
361
    else
362
        div(promote(x, y)..., r)
×
363
    end
364
end
365

366
# Integers
367
# fld(x, y) == div(x, y) - ((x >= 0) != (y >= 0) && rem(x, y) != 0 ? 1 : 0)
368
div(x::T, y::T, ::typeof(RoundDown)) where {T<:Unsigned} = div(x, y)
2,026✔
369
function div(x::T, y::T, ::typeof(RoundDown)) where T<:Integer
3✔
370
    d = div(x, y, RoundToZero)
59,122,346✔
371
    return d - (signbit(x ⊻ y) & (d * y != x))
59,126,598✔
372
end
373

374
# cld(x, y) = div(x, y) + ((x > 0) == (y > 0) && rem(x, y) != 0 ? 1 : 0)
375
function div(x::T, y::T, ::typeof(RoundUp)) where T<:Unsigned
376
    d = div(x, y, RoundToZero)
144✔
377
    return d + (d * y != x)
133✔
378
end
379
function div(x::T, y::T, ::typeof(RoundUp)) where T<:Integer
4✔
380
    d = div(x, y, RoundToZero)
1,113,378✔
381
    return d + (((x > 0) == (y > 0)) & (d * y != x))
1,115,667✔
382
end
383

384
# Real
385
# NOTE: C89 fmod() and x87 FPREM implicitly provide truncating float division,
386
# so it is used here as the basis of float div().
387
div(x::T, y::T, r::RoundingMode) where {T<:AbstractFloat} = convert(T, round((x - rem(x, y, r)) / y))
595,750✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc