#numeric #comparison #hash #order #equality

no-std num-order

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

9 releases (4 stable)

1.0.4 May 24, 2022
1.0.3 Apr 17, 2022
1.0.1 Mar 31, 2022
0.2.1 Mar 18, 2022
0.0.1 Mar 5, 2022

#3 in #equality

Download history 37/week @ 2023-02-02 23/week @ 2023-02-09 89/week @ 2023-02-16 90/week @ 2023-02-23 107/week @ 2023-03-02 108/week @ 2023-03-09 50/week @ 2023-03-16 39/week @ 2023-03-23 52/week @ 2023-03-30 70/week @ 2023-04-06 80/week @ 2023-04-13 56/week @ 2023-04-20 64/week @ 2023-04-27 118/week @ 2023-05-04 59/week @ 2023-05-11 42/week @ 2023-05-18

290 downloads per month
Used in 4 crates (2 directly)

Apache-2.0

91KB
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())

lib.rs:

num-order implements numerically consistent [Eq][core::cmp::Eq], [Ord][core::cmp::Ord] and [Hash][core:#️⃣: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 library
  • num-bigint: Support comparing against and hashing num-bigint::{BigInt, BigUint}
  • num-rational: Support comparing against and hashing num-rational::Ratio<I>, where I can be i8, i16, i32, i64, i128 and isize. Ratio<BigInt> is supported when both num-bigint and num-rational is enabled
  • num-complex: Support comparing against and hashing num-complex::{Complex32, Complex64}

Dependencies

~1MB
~18K SLoC