diff options
| author | Arne Dußin | 2020-11-18 13:00:42 +0100 |
|---|---|---|
| committer | Arne Dußin | 2020-11-18 13:00:42 +0100 |
| commit | 7ce22478d1bfb0e4fd179334e98dda5e1935bb95 (patch) | |
| tree | cefa5be4a633915961a04864cebbb354dad3e12c /src | |
| parent | cc253346c52425bea2ca9919de87e7cfa5ecea97 (diff) | |
| download | graf_karto-7ce22478d1bfb0e4fd179334e98dda5e1935bb95.tar.gz graf_karto-7ce22478d1bfb0e4fd179334e98dda5e1935bb95.zip | |
Fix silly bug in min function
Diffstat (limited to 'src')
| -rw-r--r-- | src/math/mod.rs | 28 |
1 files changed, 25 insertions, 3 deletions
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<T>(a: T, b: T) -> T +pub(crate) fn partial_max<T>(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<T>(a: T, b: T) -> T +pub(crate) fn partial_min<T>(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.); + } } |
