# JuliaLang/julia

change to oftype and define mean of empty range

bkamins committed Sep 14, 2018
1 parent 9e0b0ba commit 0aa06834a0b26d2411f5ed8c29bfb82d251defa8
Showing with 5 additions and 3 deletions.
1. +3 −3 stdlib/Statistics/src/Statistics.jl
2. +2 −0 stdlib/Statistics/test/runtests.jl
 @@ -132,7 +132,7 @@ _mean(A::AbstractArray{T}, region) where {T} = mean!(Base.reducedim_init(t -> t/ _mean(A::AbstractArray, ::Colon) = sum(A) / length(A) function mean(r::AbstractRange{<:Real}) isempty(r) && throw(ArgumentError("mean of an empty range is undefined")) isempty(r) && oftype((first(r) + last(r)) / 2, NaN) (first(r) + last(r)) / 2 end @@ -150,7 +150,7 @@ function _var(iterable, corrected::Bool, mean) y = iterate(iterable) if y === nothing T = eltype(iterable) return typeof((abs2(zero(T)) + abs2(zero(T)))/2)(NaN) return oftype((abs2(zero(T)) + abs2(zero(T)))/2, NaN) end count = 1 value, state = y @@ -267,7 +267,7 @@ varm(A::AbstractArray, m; corrected::Bool=true) = _varm(A, m, corrected, :) function _varm(A::AbstractArray{T}, m, corrected::Bool, ::Colon) where T n = length(A) n == 0 && return typeof((abs2(zero(T)) + abs2(zero(T)))/2)(NaN) n == 0 && return oftype((abs2(zero(T)) + abs2(zero(T)))/2, NaN) return centralize_sumabs2(A, m) / (n - Int(corrected)) end
 @@ -119,6 +119,8 @@ end @test f(2:0.1:n) ≈ f([2:0.1:n;]) end end @test mean(2:1) === NaN @test mean(big(2):1) isa BigFloat end @testset "var & std" begin