#[derive(NearlyOrd)]Expand description
Derives all nearly ordering traits for a custom type.
The derived traits are: NearlyOrdEps, NearlyOrdUlps, NearlyOrdTol and NearlyOrd. This trait can be derived for structs with named or unnamed fields as well as enums. To derive this trait, all types used for fields have to implemented NearlyOrdEps, NearlyOrdUlps, NearlyOrdTol and NearlyOrd.
To derive all nearly ordering traits on a type, this type also has to implement or derive all nearly equality traits NearlyEqEps, NearlyEqUlps, NearlyEqTol and NearlyEq.
To use the assert_nearly! and debug_assert_nearly! macros, your type must also implement the Debug trait.
§Example
§Same Type
If all fields have the same type:
- the epsilon tolerance will have the same type as the epsilon tolerance of the fields type. E.g., for f32 this would be f32.
- the ulps tolerance will have the same type as the ulps tolerance of the fields type. E.g., for f32 this would be i32.
use nearly::{assert_nearly, NearlyEq, NearlyOrd};
#[derive(NearlyEq, NearlyOrd, Debug)]
struct Point {
x: f32,
y: f32,
z: f32,
}
let a = Point{x: -3.4, y: 2.1, z: 1.0};
let b = Point{x: -3.4, y: 2.1, z: 1.0000008};
assert_nearly!(a <= b, eps = 0.0001);
assert_nearly!(a <= b, ulps = 8);
assert_nearly!(a <= b, eps = 0.0001, ulps = 8);
assert_nearly!(a <= b);§Different Types
If the fields have different types:
- the epsilon tolerance will have a tuple type. The tuple will consist of the epsilon types of the fields type in the same order as they are defined. E.g., for fields with the type f32, f64 and f32 this would be (f32, f64, f32).
- the ulps tolerance will have a tuple type. The tuple will consist of the ulps types of the fields type in the same order as they are defined. E.g., for fields with the type f32, f64, f32 this would be (i32, i64, i32).
use nearly::{assert_nearly, NearlyEq, NearlyOrd};
#[derive(NearlyEq, NearlyOrd, Debug)]
struct Point {
x: f32,
y: f64,
z: f32,
}
let a = Point{x: -3.4, y: 2.1, z: 1.0};
let b = Point{x: -3.4, y: 2.1, z: 1.0000008};
assert_nearly!(a <= b, eps = (0.0001, 0.000001, 0.0001));
assert_nearly!(a <= b, ulps = (8, 12, 8));
assert_nearly!(a <= b, eps = (0.0001, 0.000001, 0.0001), ulps = (8, 12, 8));
assert_nearly!(a <= b);