#comparison #numeric #order

num-cmp

Comparison between differently typed numbers

1 unstable release

Uses old Rust 2015

0.1.0 Mar 2, 2017

#1427 in Algorithms

Download history 148634/week @ 2024-07-25 135315/week @ 2024-08-01 247305/week @ 2024-08-08 174170/week @ 2024-08-15 157201/week @ 2024-08-22 146526/week @ 2024-08-29 259341/week @ 2024-09-05 188764/week @ 2024-09-12 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 268315/week @ 2024-11-07

1,153,185 downloads per month
Used in 158 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