#order #comparison #equality #hash #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

#144 in Algorithms

Download history 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 22845/week @ 2024-11-02 36742/week @ 2024-11-09 44353/week @ 2024-11-16 43606/week @ 2024-11-23 50666/week @ 2024-11-30 58446/week @ 2024-12-07 57703/week @ 2024-12-14 36986/week @ 2024-12-21 49548/week @ 2024-12-28

211,731 downloads per month
Used in 295 crates (9 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