Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only check isnan if applicable #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ function _quantilesort!(v::AbstractVector, sorted::Bool, minp::Real, maxp::Real)
sort!(v, 1, lv, Base.Sort.PartialQuickSort(lo:hi), Base.Sort.Forward)
end
if (sorted && (ismissing(v[end]) || (v[end] isa Number && isnan(v[end])))) ||
any(x -> ismissing(x) || (x isa Number && isnan(x)), v)
any(x -> ismissing(x) || (applicable(isnan, v[end]) && isnan(x)), v)
throw(ArgumentError("quantiles are undefined in presence of NaNs or missing values"))
end
return v
Expand Down Expand Up @@ -1049,7 +1049,7 @@ end

# When a ≉ b, b-a may overflow
# When a ≈ b, (1-γ)*a + γ*b may not be increasing with γ due to rounding
if isfinite(a) && isfinite(b) &&
if applicable(isfinite, a) && isfinite(a) && isfinite(b) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if applicable(isfinite, a) && isfinite(a) && isfinite(b) &&
if applicable(isfinite, a) && isfinite(a) &&
applicable(isfinite, b) && isfinite(b)

(!(a isa Number) || !(b isa Number) || a ≈ b)
return a + γ*(b-a)
else
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using Statistics, Test, Random, LinearAlgebra, SparseArrays, Dates
using Test: guardseed
using Dates

Random.seed!(123)

Expand Down Expand Up @@ -695,6 +696,8 @@ end
@test quantile(skipmissing([1, missing, 2]), 0.5) === 1.5
@test quantile([1], 0.5) === 1.0

@test quantile(Millisecond.(1:10), 0.5) == Millisecond(5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@test quantile(Millisecond.(1:10), 0.5) == Millisecond(5)
@test quantile(Millisecond.(0:10), 0.5) == Millisecond(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}]
Expand Down
Loading