From 5656156660a3cad9a036f37ab9fdf110c8985aac Mon Sep 17 00:00:00 2001 From: Vinzent Steinberg Date: Sun, 19 Nov 2017 10:31:00 +0100 Subject: [PATCH] Fix a negative underflow when calculating quantiles Fixes #5. --- src/quantile.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/quantile.rs b/src/quantile.rs index 6d52631..8b59ae1 100644 --- a/src/quantile.rs +++ b/src/quantile.rs @@ -60,9 +60,9 @@ impl Quantile { #[inline] fn linear(&self, i: usize, d: f64) -> f64 { debug_assert_eq!(d.abs(), 1.); - let s: usize = d.approx().unwrap(); - self.q[i] + d * (self.q[i + s] - self.q[i]) - / f64::approx_from(self.n[i + s] - self.n[i]).unwrap() + let sum = if d < 0. { i - 1 } else { i + 1 }; + self.q[i] + d * (self.q[sum] - self.q[i]) + / f64::approx_from(self.n[sum] - self.n[i]).unwrap() } /// Estimate the p-quantile of the population.