Skip to content

Commit

Permalink
Fix zero norm of number (#30809)
Browse files Browse the repository at this point in the history
Fixes #30631
  • Loading branch information
andreasnoack authored and KristofferC committed Feb 4, 2019
1 parent e934f12 commit 075affb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
16 changes: 15 additions & 1 deletion stdlib/LinearAlgebra/src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,21 @@ julia> norm(-2, Inf)
2
```
"""
@inline norm(x::Number, p::Real=2) = p == 0 ? (x==0 ? zero(abs(x)) : oneunit(abs(x))) : abs(x)
@inline function norm(x::Number, p::Real=2)
afx = abs(float(x))
if p == 0
if x == 0
return zero(afx)
elseif !isnan(x)
return oneunit(afx)
else
return afx
end
else
return afx
end
end

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

# special cases of opnorm
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 @@ -186,11 +186,15 @@ end
@test issymmetric(a)
@test ishermitian(one(elty))
@test det(a) == a
@test norm(a) == abs(a)
@test norm(a, 0) == 1
end

@test !issymmetric(NaN16)
@test !issymmetric(NaN32)
@test !issymmetric(NaN)
@test norm(NaN) === NaN
@test norm(NaN, 0) === NaN
end

@test rank(fill(0, 0, 0)) == 0
Expand Down

0 comments on commit 075affb

Please sign in to comment.