#comparison #numeric #order

num-cmp

Comparison between differently typed numbers

1 unstable release

Uses old Rust 2015

0.1.0 Mar 2, 2017

#51 in #comparison

Download history 90700/week @ 2024-03-14 91467/week @ 2024-03-21 95351/week @ 2024-03-28 165912/week @ 2024-04-04 128148/week @ 2024-04-11 161191/week @ 2024-04-18 129513/week @ 2024-04-25 117722/week @ 2024-05-02 122824/week @ 2024-05-09 130490/week @ 2024-05-16 125611/week @ 2024-05-23 197585/week @ 2024-05-30 148666/week @ 2024-06-06 258828/week @ 2024-06-13 137878/week @ 2024-06-20 118501/week @ 2024-06-27

695,958 downloads per month
Used in 143 crates (via jsonschema)

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