Skip to content

Commit bfa5c6b

Browse files
authored
Merge pull request #184 from JuliaStats/an/quantilemuladd
2 parents 793733e + 6bd1531 commit bfa5c6b

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/Statistics.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,8 +1035,10 @@ end
10351035
@assert n > 0 # this case should never happen here
10361036

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

10421044
if n == 1

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,11 @@ end
796796
@test quantile(v, 0.8, alpha=1.0, beta=1.0) 10.6
797797
@test quantile(v, 1.0, alpha=0.0, beta=0.0) 21.0
798798
@test quantile(v, 1.0, alpha=1.0, beta=1.0) 21.0
799+
800+
@testset "avoid some rounding" begin
801+
@test [quantile(1:10, i/9) for i in 0:9] == 1:10
802+
@test [quantile(1:14, i/13) for i in 0:13] == 1:14
803+
end
799804
end
800805

801806
# StatsBase issue 164

0 commit comments

Comments
 (0)