rs_utils/numeric.rs
1//! Numeric utilities
2
3use std;
4
5/// Returns the minimum of two partially ordered values, returning the rhs when
6/// they are incomparable.
7///
8/// This follows the convention of `f32::min` and `f32::max`, but the opposite
9/// convention is used internally by the `collision` crate.
10pub fn min_partial <S> (lhs : S, rhs : S) -> S where
11 S : Copy + PartialOrd
12{
13 match lhs.partial_cmp (&rhs) {
14 Some (std::cmp::Ordering::Less) | Some (std::cmp::Ordering::Equal)
15 => lhs,
16 _ => rhs
17 }
18}
19
20/// Returns the maximum of two partially ordered values, returning the rhs when
21/// they are incomparable.
22///
23/// This follows the convention of `f32::min` and `f32::max`, but the opposite
24/// convention is used internally by the `collision` crate.
25pub fn max_partial <S> (lhs : S, rhs : S) -> S where
26 S : Copy + PartialOrd
27{
28 match lhs.partial_cmp (&rhs) {
29 Some (std::cmp::Ordering::Greater) | Some (std::cmp::Ordering::Equal)
30 => lhs,
31 _ => rhs
32 }
33}