Function rust_examples::typing::better_cmp_f64[][src]

pub fn better_cmp_f64(a: f64, b: f64) -> Option<Ordering>
Expand description

Improved comparison function for positive f64s.

This implementation is slightly better and more idiomatic version of the naive approach. Instead of calling panic! when an argument is not positive, we return an Option<Ordering>.

Pros

  1. Clients are forced to handle None even though they know it’s never returned
  2. Each call adds a branching instruction which might get significant when called frequently
  3. If this is called frequently, the initial check if sign is positive will soon get costly

Cons

  1. Clients must handle the Option<Ordering> and it might not integrate well with other standard APIs
  2. For performance-critical applications it’s quite unfortunate that this version double checks a and b with an if to make sure the unsafe block is sound