Xsum in Rust
This crate implments xsum algorithm by Radford M. Neal (https://arxiv.org/abs/1505.05571).
xsum is able to calculate fast exact summation.
[!NOTE] ⚠️ Currently, xsum supports
f64calculation only.
Xsum Types
XsumSmall: Optimized for vectors or arrays with up to 1,000 elements.XsumLarge: Optimized for vectors or arrays with more than 1,000 elements.XsumAuto: Automatically selects the appropriate variant when the vectors or array size is unknown.XsumVariant: Provides a convenient interface for managing multiple Xsum structs.
[!TIP]
XsumAutointernally usesXsumSmallandXsumLarge.XsumAutohas runtime overhead to determine when to switch fromXsumSmalltoXsumLarge. If you already know the input size in advance, consider usingXsumVariantinstead to avoid this overhead.
Usage
add_list() to take vector or array
Calculates the sum of a small-sized vector or array.
use ;
let mut xsmall = new;
xsmall.add_list;
assert_eq!;
Calculates the sum of a large-sized vector or array (more than 1,000 elements).
use ;
let mut xlarge = new;
xlarge.add_list;
assert_eq!;
Calculates the sum of a unknown-sized vector or array.
use ;
let mut xauto = new;
xauto.add_list;
assert_eq!;
add() to take a floating point number
use ;
let mut xsmall = new;
let vec = vec!;
for v in vec
assert_eq!;
Chaining Method
use ;
let vec = vec!;
assert_eq!;
Variant
If you already know the input size in advance, you can directly select the most suitable xsum variant, avoiding unnecessary overhead.
use ;
let vec = vec!;
let mut xVariant = if vec.len < XSUM_THRESHOLD else ;
xVariant.add_list;
assert_eq!;
Comformance
xsum comforms to Javascript's Math.sumPrecise behavior.
Safety
This crate sets unsafe_code = "forbid" in Cargo.toml to ensure that only safe Rust is used.
Documentation
The doc can be found on docs.rs.