#complex-numbers #numbers #deserialize #complex #float #integer

number-general

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

33 releases

0.12.0 Aug 13, 2024
0.11.1 Dec 20, 2023
0.11.0 Nov 6, 2023
0.10.0 Jul 3, 2023
0.3.0 Feb 1, 2021

#112 in Math

Download history 24/week @ 2024-07-20 37/week @ 2024-07-27 30/week @ 2024-08-03 192/week @ 2024-08-10 39/week @ 2024-08-17 28/week @ 2024-08-24 47/week @ 2024-08-31 12/week @ 2024-09-07 41/week @ 2024-09-14 155/week @ 2024-09-21 39/week @ 2024-09-28 143/week @ 2024-10-05 328/week @ 2024-10-12 446/week @ 2024-10-19 499/week @ 2024-10-26 561/week @ 2024-11-02

1,868 downloads per month
Used in 16 crates (4 directly)

Apache-2.0

125KB
4K SLoC

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));

lib.rs:

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));

Dependencies

~1.2–2.2MB
~44K SLoC