aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/math/mod.rs28
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.);
+ }
}