diff --git a/src/Statistics.jl b/src/Statistics.jl index 0a3cba4..c6b3278 100644 --- a/src/Statistics.jl +++ b/src/Statistics.jl @@ -1024,7 +1024,7 @@ quantile!(v::AbstractVector, p::Real; sorted::Bool=false, alpha::Real=1.0, beta: # Function to perform partial sort of v for quantiles in given range function _quantilesort!(v::AbstractVector, sorted::Bool, minp::Real, maxp::Real) - isempty(v) && throw(ArgumentError("empty data vector")) + length(v) == 0 && throw(ArgumentError("empty data vector")) require_one_based_indexing(v) if !sorted @@ -1051,7 +1051,7 @@ end n = length(v) - @assert n > 0 # this case should never happen here + n > 0 || throw(ArgumentError("quantile is undefined for a length-0 data vector")) m = alpha + p * (one(alpha) - alpha - beta) # Using fma here avoids some rounding errors when aleph is an integer diff --git a/test/runtests.jl b/test/runtests.jl index 78c02ea..f3ce7c5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -709,6 +709,15 @@ end @test quantile(skipmissing([1, missing, 2]), 0.5) === 1.5 @test quantile([1], 0.5) === 1.0 + # length overflows to 0. Same error as an empty vector. + let r = typemin(Int):typemax(Int) + @test length(r) == 0 + @test !isempty(r) + @test_throws ArgumentError("empty data vector") quantile!([0], r, [1]) + @test_throws ArgumentError("empty data vector") quantile(r, 0.5; sorted=true) + end + @test_throws ArgumentError quantile(Int[], 0.5) + # make sure that type inference works correctly in normal cases for T in [Int, BigInt, Float64, Float16, BigFloat, Rational{Int}, Rational{BigInt}] for S in [Float64, Float16, BigFloat, Rational{Int}, Rational{BigInt}]