From b7402775aba02e22533c8dc6df897dd082471eb3 Mon Sep 17 00:00:00 2001 From: Daniel Karrasch Date: Wed, 24 Jul 2019 23:15:19 +0200 Subject: [PATCH] [Statistics] fix type determination in corm (#32271) * [Statistics] fix type determination in corm * remove obsolete typeof * use first element(s) for type initialization * add test for inhomogeneous data and for overflow * fix test with NaN (cherry picked from commit a8a567e72b669525dc4d3a38af275fe042f44a42) --- stdlib/Statistics/src/Statistics.jl | 4 ++-- stdlib/Statistics/test/runtests.jl | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/stdlib/Statistics/src/Statistics.jl b/stdlib/Statistics/src/Statistics.jl index c360b3984e7c8..f6ac5a3cd0efb 100644 --- a/stdlib/Statistics/src/Statistics.jl +++ b/stdlib/Statistics/src/Statistics.jl @@ -583,8 +583,8 @@ function corm(x::AbstractVector, mx, y::AbstractVector, my) @inbounds begin # Initialize the accumulators - xx = zero(sqrt(abs2(x[1]))) - yy = zero(sqrt(abs2(y[1]))) + xx = zero(sqrt(abs2(one(x[1])))) + yy = zero(sqrt(abs2(one(y[1])))) xy = zero(x[1] * y[1]') @simd for i in eachindex(x, y) diff --git a/stdlib/Statistics/test/runtests.jl b/stdlib/Statistics/test/runtests.jl index aa54541d0c1fe..f65256cf674da 100644 --- a/stdlib/Statistics/test/runtests.jl +++ b/stdlib/Statistics/test/runtests.jl @@ -420,6 +420,8 @@ end @test cor(repeat(1:17, 1, 17))[2] <= 1.0 @test cor(1:17, 1:17) <= 1.0 @test cor(1:17, 18:34) <= 1.0 + @test cor(Any[1, 2], Any[1, 2]) == 1.0 + @test isnan(cor([0], Int8[81])) let tmp = range(1, stop=85, length=100) tmp2 = Vector(tmp) @test cor(tmp, tmp) <= 1.0