#order #comparison #hash #equality #big-integer #hash-values #numeric

no-std num-order

Numerically consistent Eq, Ord and Hash implementations for various num types (u32, f64, num_bigint::BigInt, etc.)

11 releases (6 stable)

1.2.0 Aug 31, 2023
1.1.0 Aug 29, 2023
1.0.4 May 24, 2022
1.0.3 Apr 17, 2022
0.0.1 Mar 5, 2022

#295 in Algorithms

Download history 466/week @ 2024-07-20 411/week @ 2024-07-27 579/week @ 2024-08-03 553/week @ 2024-08-10 1120/week @ 2024-08-17 1395/week @ 2024-08-24 1105/week @ 2024-08-31 1565/week @ 2024-09-07 1604/week @ 2024-09-14 2607/week @ 2024-09-21 5491/week @ 2024-09-28 6213/week @ 2024-10-05 4672/week @ 2024-10-12 4190/week @ 2024-10-19 13476/week @ 2024-10-26 22834/week @ 2024-11-02

46,273 downloads per month
Used in 277 crates (7 directly)

Apache-2.0

97KB
2K SLoC

Numerically consistent Eq, Ord and Hash implementations for various num types (u32, f64, num_bigint::BigInt, etc.).

Example

use std::cmp::Ordering;
use std::hash::Hasher;
use std::collections::hash_map::DefaultHasher;
use num_order::{NumOrd, NumHash};

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);

// 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())

Dependencies

~145–315KB