Skip to content

Commit

Permalink
Merge pull request JuliaLang#12564 from stevengj/norm_nan
Browse files Browse the repository at this point in the history
fix JuliaLang#12552 (norm should return NaN for NaN inputs)
  • Loading branch information
stevengj committed Aug 11, 2015
2 parents 03eb136 + e7e614d commit f74f6ee
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
6 changes: 4 additions & 2 deletions base/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ function generic_vecnormMinusInf(x)
minabs = norm(v)
while !done(x, s)
(v, s) = next(x, s)
minabs = Base.scalarmin(minabs, norm(v))
vnorm = norm(v)
minabs = ifelse(isnan(minabs) | (minabs < vnorm), minabs, vnorm)
end
return float(minabs)
end
Expand All @@ -85,7 +86,8 @@ function generic_vecnormInf(x)
maxabs = norm(v)
while !done(x, s)
(v, s) = next(x, s)
maxabs = Base.scalarmax(maxabs, norm(v))
vnorm = norm(v)
maxabs = ifelse(isnan(maxabs) | (maxabs > vnorm), maxabs, vnorm)
end
return float(maxabs)
end
Expand Down
1 change: 1 addition & 0 deletions base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ function -(x::BigFloat)
end

function sqrt(x::BigFloat)
isnan(x) && return x
z = BigFloat()
ccall((:mpfr_sqrt, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}, Int32), &z, &x, ROUNDING_MODE[end])
if isnan(z)
Expand Down
8 changes: 8 additions & 0 deletions test/linalg/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ for elty in (Float32, Float64, BigFloat, Complex{Float32}, Complex{Float64}, Com
@test_approx_eq norm(xs, 3) cbrt(5)
@test_approx_eq norm(xs, Inf) 1

# Issue #12552:
if real(elty) <: AbstractFloat
for p in [-Inf,-1,1,2,3,Inf]
@test isnan(norm(elty[0,NaN],p))
@test isnan(norm(elty[NaN,0],p))
end
end

## Number
norm(x[1:1]) === norm(x[1], -Inf)
norm(x[1:1]) === norm(x[1], 0)
Expand Down
2 changes: 2 additions & 0 deletions test/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -861,3 +861,5 @@ let b = IOBuffer()
seekstart(b)
@test deserialize(b) == x
end

@test isnan(sqrt(BigFloat(NaN)))

0 comments on commit f74f6ee

Please sign in to comment.