#comparison #numeric #order

num-cmp

Comparison between differently typed numbers

1 unstable release

Uses old Rust 2015

0.1.0 Mar 2, 2017

#1391 in Algorithms

Download history 191521/week @ 2024-09-19 190278/week @ 2024-09-26 217469/week @ 2024-10-03 202486/week @ 2024-10-10 356820/week @ 2024-10-17 259423/week @ 2024-10-24 225200/week @ 2024-10-31 269135/week @ 2024-11-07 248030/week @ 2024-11-14 223508/week @ 2024-11-21 152753/week @ 2024-11-28 232174/week @ 2024-12-05 242730/week @ 2024-12-12 113911/week @ 2024-12-19 59989/week @ 2024-12-26 180599/week @ 2025-01-02

651,692 downloads per month
Used in 174 crates (2 directly)

MIT/Apache

43KB
892 lines

num-cmp

The NumCmp trait for comparison between differently typed numbers.

use std::f32;
use std::cmp::Ordering;
use num_cmp::NumCmp;

assert!(NumCmp::num_eq(3u64, 3.0f32));
assert!(NumCmp::num_lt(-4.7f64, -4i8));
assert!(!NumCmp::num_ge(-3i8, 1u16));

// 40_000_000 can be exactly represented in f32, 40_000_001 cannot
assert_eq!(NumCmp::num_cmp(40_000_000.0f32, 40_000_000u32), Some(Ordering::Equal));
assert_ne!(NumCmp::num_cmp(40_000_001.0f32, 40_000_001u32), Some(Ordering::Equal));
assert_eq!(NumCmp::num_cmp(f32::NAN,        40_000_002u32), None);

The i128 Cargo feature can be enabled in nightly to get support for i128 and u128 types as well, which is being implemented in Rust issue #35118.


lib.rs:

The NumCmp trait for comparison between differently typed numbers.

use std::f32;
use std::cmp::Ordering;
use num_cmp::NumCmp;

assert!(NumCmp::num_eq(3u64, 3.0f32));
assert!(NumCmp::num_lt(-4.7f64, -4i8));
assert!(!NumCmp::num_ge(-3i8, 1u16));

// 40_000_000 can be exactly represented in f32, 40_000_001 cannot
assert_eq!(NumCmp::num_cmp(40_000_000.0f32, 40_000_000u32), Some(Ordering::Equal));
assert_ne!(NumCmp::num_cmp(40_000_001.0f32, 40_000_001u32), Some(Ordering::Equal));
assert_eq!(NumCmp::num_cmp(f32::NAN,        40_000_002u32), None);

The i128 Cargo feature can be enabled in nightly to get support for i128 and u128 types as well, which is being implemented in Rust issue #35118.

No runtime deps

Features