From a8a567e72b669525dc4d3a38af275fe042f44a42 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 --- 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 19e73e6ab13bf..d16340aaa4d30 100644 --- a/stdlib/Statistics/src/Statistics.jl +++ b/stdlib/Statistics/src/Statistics.jl @@ -645,8 +645,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 1f1020970a5ae..fb77c535c299f 100644 --- a/stdlib/Statistics/test/runtests.jl +++ b/stdlib/Statistics/test/runtests.jl @@ -460,6 +460,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