Skip to content

Commit

Permalink
Add missing support in isapprox (JuliaLang#28094)
Browse files Browse the repository at this point in the history
* Add `missing` support in `isapprox`

`isapprox` now behaves the same way as `==` in the presence of missing
values: if either argument is `missing`, the result is `missing`.

Fixes JuliaLang#28088.

* Define `norm` for missing values
  • Loading branch information
ararslan authored and stevengj committed Jul 22, 2018
1 parent 03f0ae5 commit 39eb68b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
7 changes: 5 additions & 2 deletions base/missing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,17 @@ isequal(::Any, ::Missing) = false
isless(::Missing, ::Missing) = false
isless(::Missing, ::Any) = false
isless(::Any, ::Missing) = true
isapprox(::Missing, ::Missing; kwargs...) = missing
isapprox(::Missing, ::Any; kwargs...) = missing
isapprox(::Any, ::Missing; kwargs...) = missing

# Unary operators/functions
for f in (:(!), :(~), :(+), :(-), :(identity), :(zero), :(one), :(oneunit),
:(isfinite), :(isinf), :(isodd),
:(isinteger), :(isreal), :(isnan),
:(iszero), :(transpose), :(adjoint), :(float), :(conj),
:(abs), :(abs2), :(iseven), :(ispow2),
:(real), :(imag), :(sign))
:(abs), :(abs2), :(iseven), :(ispow2),
:(real), :(imag), :(sign))
@eval ($f)(::Missing) = missing
end
for f in (:(Base.zero), :(Base.one), :(Base.oneunit))
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ julia> norm(-2, Inf)
```
"""
@inline norm(x::Number, p::Real=2) = p == 0 ? (x==0 ? zero(abs(x)) : oneunit(abs(x))) : abs(x)

norm(::Missing, p::Real=2) = missing

# special cases of opnorm
function opnorm1(A::AbstractMatrix{T}) where T
Expand Down
4 changes: 4 additions & 0 deletions stdlib/LinearAlgebra/test/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,8 @@ end
end
end

@testset "missing values" begin
@test ismissing(norm(missing))
end

end # module TestGeneric
3 changes: 3 additions & 0 deletions test/missing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ end
@test !isless(missing, missing)
@test !isless(missing, 1)
@test isless(1, missing)
@test (missing missing) === missing
@test isapprox(missing, 1.0, atol=1e-6) === missing
@test isapprox(1.0, missing, rtol=1e-6) === missing

@test !any(T -> T === Union{Missing,Bool}, Base.return_types(isequal, Tuple{Any,Any}))
end
Expand Down

0 comments on commit 39eb68b

Please sign in to comment.