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