From e7e614d088197836c41cabb61b4078cc06c9a6c9 Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Tue, 11 Aug 2015 12:41:43 -0400 Subject: [PATCH] fix #12552 (norm should return NaN for NaN inputs); also fix sqrt(big(NaN)) --- base/linalg/generic.jl | 6 ++++-- base/mpfr.jl | 1 + test/linalg/dense.jl | 8 ++++++++ test/mpfr.jl | 2 ++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/base/linalg/generic.jl b/base/linalg/generic.jl index 428376b673d08..d7b76cb77b363 100644 --- a/base/linalg/generic.jl +++ b/base/linalg/generic.jl @@ -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 @@ -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 diff --git a/base/mpfr.jl b/base/mpfr.jl index a773cd39c302e..5ac9dcf965300 100644 --- a/base/mpfr.jl +++ b/base/mpfr.jl @@ -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) diff --git a/test/linalg/dense.jl b/test/linalg/dense.jl index 5209985db5e47..68e72f52eecbf 100644 --- a/test/linalg/dense.jl +++ b/test/linalg/dense.jl @@ -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) diff --git a/test/mpfr.jl b/test/mpfr.jl index 7540eb59c6e22..f4849a3878d06 100644 --- a/test/mpfr.jl +++ b/test/mpfr.jl @@ -861,3 +861,5 @@ let b = IOBuffer() seekstart(b) @test deserialize(b) == x end + +@test isnan(sqrt(BigFloat(NaN)))