Skip to content

Commit c38298a

Browse files
KlausCmbauman
authored andcommitted
Wrong evaluation in sparse/higherorderfns.jl fix#23857 (#24806)
* wrong evaluation in sparse/higherorderfns.jl fix#23857 * `_broadcast_nonzeropre` fixed all branches of if-statement
1 parent 64de190 commit c38298a

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

stdlib/SparseArrays/src/higherorderfns.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ function _broadcast_notzeropres!(f::Tf, fillvalue, C::SparseVecOrMat, A::SparseV
688688
Bk, stopBk = numcols(B) == 1 ? (colstartind(B, 1), colboundind(B, 1)) : (colstartind(B, j), colboundind(B, j))
689689
Ax = Ak < stopAk ? storedvals(A)[Ak] : zero(eltype(A))
690690
fvAzB = f(Ax, zero(eltype(B)))
691-
if _iszero(fvAzB)
691+
if fvAzB == fillvalue
692692
while Bk < stopBk
693693
Cx = f(Ax, storedvals(B)[Bk])
694694
Cx != fillvalue && (storedvals(C)[jo + storedinds(B)[Bk]] = Cx)
@@ -713,7 +713,7 @@ function _broadcast_notzeropres!(f::Tf, fillvalue, C::SparseVecOrMat, A::SparseV
713713
Bk, stopBk = numcols(B) == 1 ? (colstartind(B, 1), colboundind(B, 1)) : (colstartind(B, j), colboundind(B, j))
714714
Bx = Bk < stopBk ? storedvals(B)[Bk] : zero(eltype(B))
715715
fzAvB = f(zero(eltype(A)), Bx)
716-
if _iszero(fzAvB)
716+
if fzAvB == fillvalue
717717
while Ak < stopAk
718718
Cx = f(storedvals(A)[Ak], Bx)
719719
Cx != fillvalue && (storedvals(C)[jo + storedinds(A)[Ak]] = Cx)

stdlib/SparseArrays/test/higherorderfns.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,21 @@ end
215215
end
216216
end
217217
end
218+
219+
# fix#23857
220+
@test sparse([1; 0]) ./ [1] == sparse([1.0; 0.0])
221+
@test isequal(sparse([1 2; 1 0]) ./ [1; 0], sparse([1.0 2; Inf NaN]))
222+
@test sparse([1 0]) ./ [1] == sparse([1.0 0.0])
223+
@test isequal(sparse([1 2; 1 0]) ./ [1 0], sparse([1.0 Inf; 1 NaN]))
224+
225+
@test sparse([1]) .\ sparse([1; 0]) == sparse([1.0; 0.0])
226+
@test isequal(sparse([1; 0]) .\ sparse([1 2; 1 0]), sparse([1.0 2; Inf NaN]))
227+
@test sparse([1]) .\ sparse([1 0]) == sparse([1.0 0.0])
228+
@test isequal(sparse([1 0]) .\ sparse([1 2; 1 0]), sparse([1.0 Inf; 1 NaN]))
229+
218230
end
219231

232+
220233
@testset "broadcast[!] implementation capable of handling >2 (input) sparse vectors/matrices" begin
221234
N, M, p = 10, 12, 0.3
222235
f(x, y, z) = x + y + z + 1

0 commit comments

Comments
 (0)