From 7ce22478d1bfb0e4fd179334e98dda5e1935bb95 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Wed, 18 Nov 2020 13:00:42 +0100 Subject: Fix silly bug in min function --- src/math/mod.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'src/math') diff --git a/src/math/mod.rs b/src/math/mod.rs index afbc4a3..0b591d7 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -34,7 +34,7 @@ pub fn round(num: f32, step: f32) -> f32 { /// designed so functions that should be able to use f32 and f64 work, eventhough these do not /// implement Ord. The downside of this function however is, that its behaviour is undefined when /// `f32::NaN` for instance were to be passed. -fn partial_max(a: T, b: T) -> T +pub(crate) fn partial_max(a: T, b: T) -> T where T: PartialOrd, { @@ -44,9 +44,31 @@ where } } /// Like `partial_max`, but for minimum values. Comes with the same downside, too. -fn partial_min(a: T, b: T) -> T +pub(crate) fn partial_min(a: T, b: T) -> T where T: PartialOrd, { - partial_max(b, a) + match a.partial_cmp(&b) { + Some(Ordering::Less) => a, + _ => b, + } +} + +#[cfg(test)] +mod test { + #[test] + fn partial_max() { + assert_eq!(super::partial_max(0., 0.), 0.); + assert_eq!(super::partial_max(-1., 1.), 1.); + assert_eq!(super::partial_max(-2., -1.), -1.); + assert_eq!(super::partial_max(2., 1.), 2.); + } + + #[test] + fn partial_min() { + assert_eq!(super::partial_min(0., 0.), 0.); + assert_eq!(super::partial_min(-1., 1.), -1.); + assert_eq!(super::partial_min(-2., -1.), -2.); + assert_eq!(super::partial_min(2., 1.), 1.); + } } -- cgit v1.2.3-70-g09d2