Function rust_examples::typing::better_cmp_f64 [−][src]
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
- Clients are forced to handle None even though they know it’s never returned
- Each call adds a branching instruction which might get significant when called frequently
- If this is called frequently, the initial check if sign is positive will soon get costly
Cons
- Clients must handle the
Option<Ordering>and it might not integrate well with other standard APIs - For performance-critical applications it’s quite unfortunate that this version double checks
aandbwith anifto make sure theunsafeblock is sound
