Crates.io | fsum |
lib.rs | fsum |
version | 0.1.2 |
source | src |
created_at | 2022-02-21 22:35:57.625773 |
updated_at | 2022-02-25 13:47:48.272941 |
description | The library to calculate accurate sum of floats. |
homepage | |
repository | https://github.com/beling/bsuccinct-rs |
max_upload_size | |
id | 537010 |
size | 10,406 |
fsum
is the Rust library by Piotr Beling to calculate accurate sum of floats.
use fsum::FSum;
assert_eq!(FSum::new().add(1e100).add(1.0).add(-1e100).value(), 1.0);
assert_eq!(FSum::with_all((0..10).map(|_| 0.1)).value(), 1.0);
assert_eq!(FSum::with_all(&[1e100, 1.0, -1e100]).value(), 1.0);
let mut s = FSum::new();
assert_eq!(s.value(), 0.0);
s += 3.0;
assert_eq!(s.value(), 3.0);
s -= 1.0;
assert_eq!(s.value(), 2.0);
The complexities of summing n numbers are:
Usually the complexities are close to optimistic.
Calculation code bases on (is mostly copied from) sum
method
of test::stats::Stats
implementation for f64
(which probably reimplements math.fsum
from Python's library)
and source of CPython.
See also:
The method sacrifices performance at the altar of accuracy Depends on IEEE-754 arithmetic guarantees. See proof of the correctness in Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates