Expand description
num-order
implements numerically consistent Eq, Ord and
Hash for various num
types.
use std::cmp::Ordering;
use std::hash::Hasher;
use std::collections::hash_map::DefaultHasher;
use num_order::NumOrd;
assert!(NumOrd::num_eq(&3u64, &3.0f32));
assert!(NumOrd::num_lt(&-4.7f64, &-4i8));
assert!(!NumOrd::num_ge(&-3i8, &1u16));
// 40_000_000 can be exactly represented in f32, 40_000_001 cannot
// 40_000_001 becames 40_000_000.0 in f32
assert_eq!(NumOrd::num_cmp(&40_000_000f32, &40_000_000u32), Ordering::Equal);
assert_ne!(NumOrd::num_cmp(&40_000_001f32, &40_000_001u32), Ordering::Equal);
assert_eq!(NumOrd::num_partial_cmp(&f32::NAN, &40_000_002u32), None);
use num_order::NumHash;
// same hash values are guaranteed for equal numbers
let mut hasher1 = DefaultHasher::new();
3u64.num_hash(&mut hasher1);
let mut hasher2 = DefaultHasher::new();
3.0f32.num_hash(&mut hasher2);
assert_eq!(hasher1.finish(), hasher2.finish())
This crate can serve applications where float-ord,
num-cmp, num-ord are used.
Meanwhile it also supports hashing and more numeric types (num-bigint
, etc.).
Optional Features
std
: enable std librarynum-bigint
: Support comparing against and hashingnum-bigint::{BigInt, BigUint}
num-rational
: Support comparing against and hashingnum-rational::Ratio<I>
, whereI
can bei8
,i16
,i32
,i64
,i128
andisize
.Ratio<BigInt>
is supported when bothnum-bigint
andnum-rational
is enablednum-complex
: Support comparing against and hashingnum-complex::{Complex32, Complex64}
Traits
- Consistent hash implementation among different numeric types.
- Consistent comparison among different numeric types.