Skip to content

Fix overflow for Midpoint #28

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

Merged
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
10 changes: 9 additions & 1 deletion src/maybe_nan/impl_not_none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::NotNone;
use num_traits::{FromPrimitive, ToPrimitive};
use std::cmp;
use std::fmt;
use std::ops::{Add, Deref, DerefMut, Div, Mul, Sub};
use std::ops::{Add, Deref, DerefMut, Div, Mul, Sub, Rem};

impl<T> Deref for NotNone<T> {
type Target = T;
Expand Down Expand Up @@ -96,6 +96,14 @@ impl<T: Div> Div for NotNone<T> {
}
}

impl<T: Rem> Rem for NotNone<T> {
type Output = NotNone<T::Output>;
#[inline]
fn rem(self, rhs: Self) -> Self::Output {
self.map(|v| v.rem(rhs.unwrap()))
}
}

impl<T: ToPrimitive> ToPrimitive for NotNone<T> {
#[inline]
fn to_isize(&self) -> Option<isize> {
Expand Down
16 changes: 11 additions & 5 deletions src/quantile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use {MaybeNan, MaybeNanExt, Sort1dExt};
pub mod interpolate {
use ndarray::azip;
use ndarray::prelude::*;
use num_traits::{FromPrimitive, ToPrimitive};
use std::ops::{Add, Div};
use num_traits::{FromPrimitive, ToPrimitive, NumOps};

/// Used to provide an interpolation strategy to [`quantile_axis_mut`].
///
Expand Down Expand Up @@ -116,7 +115,7 @@ pub mod interpolate {

impl<T> Interpolate<T> for Midpoint
where
T: Add<T, Output = T> + Div<T, Output = T> + Clone + FromPrimitive,
T: NumOps + Clone + FromPrimitive,
{
fn needs_lower(_q: f64, _len: usize) -> bool {
true
Expand All @@ -134,13 +133,20 @@ pub mod interpolate {
D: Dimension,
{
let denom = T::from_u8(2).unwrap();
(lower.unwrap() + higher.unwrap()).mapv_into(|x| x / denom.clone())
let mut lower = lower.unwrap();
let higher = higher.unwrap();
azip!(
mut lower, ref higher in {
*lower = lower.clone() + (higher.clone() - lower.clone()) / denom.clone()
}
);
lower
}
}

impl<T> Interpolate<T> for Linear
where
T: Add<T, Output = T> + Clone + FromPrimitive + ToPrimitive,
T: NumOps + Clone + FromPrimitive + ToPrimitive,
{
fn needs_lower(_q: f64, _len: usize) -> bool {
true
Expand Down
11 changes: 11 additions & 0 deletions tests/quantile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ndarray::prelude::*;
use ndarray_stats::{
interpolate::{Higher, Linear, Lower, Midpoint, Nearest},
QuantileExt,
Quantile1dExt,
};

#[test]
Expand Down Expand Up @@ -148,3 +149,13 @@ fn test_quantile_axis_skipnan_mut_linear_opt_i32() {
assert_eq!(q[0], Some(3));
assert!(q[1].is_none());
}

#[test]
fn test_midpoint_overflow() {
// Regression test
// This triggered an overflow panic with a naive Midpoint implementation: (a+b)/2
let mut a: Array1<u8> = array![129, 130, 130, 131];
let median = a.quantile_mut::<Midpoint>(0.5).unwrap();
let expected_median = 130;
assert_eq!(median, expected_median);
}