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

Use muladd in aleph calculation in _quantile to avoid some rounding errors #184

Merged
merged 2 commits into from
Jan 3, 2025
Merged
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
6 changes: 4 additions & 2 deletions src/Statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1035,8 +1035,10 @@ end
@assert n > 0 # this case should never happen here

m = alpha + p * (one(alpha) - alpha - beta)
aleph = n*p + oftype(p, m)
j = clamp(trunc(Int, aleph), 1, n-1)
# Using fma here avoids some rounding errors when aleph is an integer
# The use of oftype supresses the promotion caused by alpha and beta
aleph = fma(n, p, oftype(p, m))
j = clamp(trunc(Int, aleph), 1, n - 1)
γ = clamp(aleph - j, 0, 1)

if n == 1
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,11 @@ end
@test quantile(v, 0.8, alpha=1.0, beta=1.0) 10.6
@test quantile(v, 1.0, alpha=0.0, beta=0.0) 21.0
@test quantile(v, 1.0, alpha=1.0, beta=1.0) 21.0

@testset "avoid some rounding" begin
@test [quantile(1:10, i/9) for i in 0:9] == 1:10
@test [quantile(1:14, i/13) for i in 0:13] == 1:14
end
end

# StatsBase issue 164
Expand Down
Loading