Skip to content

Commit 7457d72

Browse files
committed
Handle empty arrays
1 parent c0e8dcc commit 7457d72

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/generic.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2004,7 +2004,12 @@ function isapprox(x::AbstractArray, y::AbstractArray;
20042004
atol::Real=0,
20052005
rtol::Real=Base.rtoldefault(promote_leaf_eltypes(x),promote_leaf_eltypes(y),atol),
20062006
nans::Bool=false, norm::Function=norm)
2007-
d = norm_x_minus_y(x, y)
2007+
d = if isempty(x) && isempty(y)
2008+
Base.promote_shape(size(x), size(y)) # ensure same size
2009+
norm(zero(eltype(x)) - zero(eltype(y)))
2010+
else
2011+
norm_x_minus_y(x, y)
2012+
end
20082013
if isfinite(d)
20092014
return iszero(rtol) ? d <= atol : d <= max(atol, rtol*max(norm(x), norm(y)))
20102015
else

test/generic.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,11 +938,12 @@ end
938938
@test B == A2
939939
end
940940

941-
@testset "isapprox alloc" begin
941+
@testset "isapprox for Arrays" begin
942942
A = rand(3,3)
943943
isapprox(A, A)
944944
n = @allocated isapprox(A, A)
945945
@test n == 0
946+
@test Int[] Int[]
946947
end
947948

948949
end # module TestGeneric

0 commit comments

Comments
 (0)