| Crates.io | xsum |
| lib.rs | xsum |
| version | 0.1.6 |
| created_at | 2025-06-05 16:43:27.11572+00 |
| updated_at | 2025-10-20 23:13:36.54783+00 |
| description | Fast Exact Summation of Floating-Point Numbers |
| homepage | https://github.com/Gumichocopengin8/xsum.rs |
| repository | https://github.com/Gumichocopengin8/xsum.rs |
| max_upload_size | |
| id | 1701810 |
| size | 84,254 |
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.
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.
add_list() to take vector or arrayCalculates the sum of a small-sized vector or array.
use xsum::{Xsum, XsumSmall};
let mut xsmall = XsumSmall::new();
xsmall.add_list(&vec![1.0, 2.0, 3.0]);
assert_eq!(xsmall.sum(), 6.0);
Calculates the sum of a large-sized vector or array (more than 1,000 elements).
use xsum::{Xsum, XsumLarge};
let mut xlarge = XsumLarge::new();
xlarge.add_list(&vec![1.0; 1_000]);
assert_eq!(xlarge.sum(), 1000.0);
Calculates the sum of a unknown-sized vector or array.
use xsum::{Xsum, XsumAuto};
let mut xauto = XsumAuto::new();
xauto.add_list(&vec![1.0; 1_000]);
assert_eq!(xauto.sum(), 1000.0);
add() to take a floating point numberuse xsum::{Xsum, XsumSmall};
let mut xsmall = XsumSmall::new();
let vec = vec![1.0, 2.0, 3.0];
for v in vec {
xsmall.add(v);
}
assert_eq!(xsmall.sum(), 6.0);
use xsum::{Xsum, XsumExt};
let vec = vec![1.0, 2.0, 3.0];
assert_eq!(vec.xsum(), 6.0);
If you already know the input size in advance, you can directly select the most suitable xsum variant, avoiding unnecessary overhead.
use xsum::{Xsum, XsumVariant, XsumAuto, XsumLarge, XsumSmall, constants::XSUM_THRESHOLD};
let vec = vec![1.0; 2_000];
let mut xVariant = if vec.len() < XSUM_THRESHOLD {
XsumVariant::Small(XsumSmall::new())
} else {
XsumVariant::Large(XsumLarge::new())
};
xVariant.add_list(&vec);
assert_eq!(xVariant.sum(), 2_000.0);
xsum comforms to Javascript's Math.sumPrecise behavior.
This crate sets unsafe_code = "forbid" in Cargo.toml to ensure that only safe Rust is used.
The doc can be found on docs.rs.