#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

#245 in Algorithms

Download history 53/week @ 2022-03-04 30/week @ 2022-03-11 57/week @ 2022-03-18 14/week @ 2022-03-25 50/week @ 2022-04-01 24/week @ 2022-04-08 23/week @ 2022-04-15 5/week @ 2022-04-22 60/week @ 2022-04-29 92/week @ 2022-05-06 145/week @ 2022-05-13 50/week @ 2022-05-20 68/week @ 2022-05-27 67/week @ 2022-06-03 2/week @ 2022-06-10 7/week @ 2022-06-17

147 downloads per month
Used in 2 crates (via suan-core)

Apache-2.0

90KB
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