#comparison #numeric

num-cmp

Comparison between differently typed numbers

1 unstable release

Uses old Rust 2015

0.1.0 Mar 2, 2017

#1198 in Algorithms

Download history 53304/week @ 2023-12-17 17916/week @ 2023-12-24 71843/week @ 2023-12-31 68937/week @ 2024-01-07 76143/week @ 2024-01-14 81934/week @ 2024-01-21 67378/week @ 2024-01-28 70928/week @ 2024-02-04 75459/week @ 2024-02-11 79165/week @ 2024-02-18 89219/week @ 2024-02-25 103978/week @ 2024-03-03 86792/week @ 2024-03-10 88491/week @ 2024-03-17 93693/week @ 2024-03-24 98083/week @ 2024-03-31

371,100 downloads per month
Used in 124 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