#unsigned-integer #complex-numbers #unsigned #integer #float #operation #numbers

number-general

A generic number type for Rust which supports basic math and (de)serialization

34 releases

0.13.0 Feb 17, 2025
0.12.0 Aug 13, 2024
0.11.1 Dec 20, 2023
0.11.0 Nov 6, 2023
0.3.0 Feb 1, 2021

#302 in Math

Download history 72/week @ 2025-01-03 147/week @ 2025-01-10 168/week @ 2025-01-17 179/week @ 2025-01-24 230/week @ 2025-01-31 307/week @ 2025-02-07 300/week @ 2025-02-14 201/week @ 2025-02-21 181/week @ 2025-02-28 116/week @ 2025-03-07 182/week @ 2025-03-14 139/week @ 2025-03-21 144/week @ 2025-03-28 37/week @ 2025-04-04 91/week @ 2025-04-11 56/week @ 2025-04-18

338 downloads per month
Used in 16 crates (4 directly)

Apache-2.0

125KB
4K SLoC

Provides a generic Number enum with Boolean, Complex, Float, [Int], and UInt variants, as well as a NumberCollator, ComplexCollator, and FloatCollator since these types do not implement [Ord].

Number supports casting with safecast.

For (de)serialization with serde, enable the "serde" feature.

For (de)coding with destream, enable the "stream" feature.

Example usage:

let sequence: Vec<Number> = vec![true.into(), 2.into(), 3.5.into(), [1.0, -0.5].into()];
let actual = sequence.into_iter().product();
let expected = Number::from(num::Complex::<f64>::new(7., -3.5));

assert_eq!(expected, actual);
assert_eq!(Int::cast_from(actual), Int::from(7));

number-general

A generic Rust number type with support for basic math operations, (de)serialization, and casting with safecast.

Example usage:

use number_general::{Int, Number};
use safecast::CastFrom;

let sequence: Vec<Number> = serde_json::from_str("[true, 2, 3.5, -4, [1.0, -0.5]]").unwrap();
let actual = sequence.into_iter().product();

assert_eq!(actual, Number::from(num::Complex::<f64>::new(-28., 14.)));
assert_eq!(Int::cast_from(actual), Int::from(-28));

Dependencies

~1.1–2.2MB
~43K SLoC