dactyl

Macro total_cmp

Source
macro_rules! total_cmp {
    ($a:ident   $op:tt $b:ident) => { ... };
    ($a:ident   $op:tt $b:literal) => { ... };
    ($a:ident   $op:tt ($b:expr)) => { ... };
    ($a:literal $op:tt $b:ident) => { ... };
    ($a:literal $op:tt $b:literal) => { ... };
    ($a:literal $op:tt ($b:expr)) => { ... };
    (($a:expr)  $op:tt $b:ident) => { ... };
    (($a:expr)  $op:tt $b:literal) => { ... };
    (($a:expr)  $op:tt ($b:expr)) => { ... };
}
Expand description

§“Total” Float Comparison.

This macro allows you to compare two floats via f32::total_cmp/f64::total_cmp using PartialEq/PartialOrd-like syntax.

The floats can be idents, literals, and/or parenthesized expressions. Mixing and matching is allowed, so long as both sides resolve to the same type (f32 or f64).

The following comparison operators are supported:

  • <
  • <=
  • ==
  • >=
  • >
  • !=

§Examples

use dactyl::total_cmp;

let a: f64 = 1.0;
let b: f64 = 2.0;
let c: f64 = 3.0;

// Less than.
assert!(total_cmp!(a < b));
assert!(total_cmp!(-3.0_f64 < a)); // Literals are fine too.
assert!(! total_cmp!(c < a));      // Nope!

// Less than or equal to.
assert!(total_cmp!(a <= a));
assert!(total_cmp!(b <= c));
assert!(! total_cmp!(c <= b)); // Nope!

// Equal.
assert!(total_cmp!(a == a));
assert!(total_cmp!(b == b));

// Not equal.
assert!(total_cmp!(a != c));
assert!(! total_cmp!(a != a)); // Nope!

// Greater than or equal to.
assert!(total_cmp!(a >= a));
assert!(total_cmp!(c >= b));
assert!(! total_cmp!(b >= c)); // Nope!

// Greater than.
assert!(total_cmp!(b > a));
assert!(total_cmp!(c > b));
assert!(! total_cmp!(b > b));  // Nope!

Expressions — or anything Rust doesn’t consider to be an ident or literal — work the same way, but need to be wrapped in parentheses first:

use dactyl::total_cmp;

assert!(total_cmp!((f64::NEG_INFINITY) < 0_f64));
assert!(total_cmp!((1_f64 + 2_f64) == 3_f64));
assert!(total_cmp!(1_f64 > (2_f64.mul_add(2.0, -3.5))));

Total comparison doesn’t always agree with partial comparison. Refer to f64::total_cmp for more details, but here’s one obvious difference:

use dactyl::total_cmp;

assert!(total_cmp!(-0_f32 < 0_f32)); // Total cmp honors the negative.
assert_eq!(-0_f32, 0_f32);           // Partial cmp ignores it.