#sum #accurate #f64 #float #exact

fsum

The library to calculate accurate sum of floats

3 releases

0.1.2 Feb 25, 2022
0.1.1 Feb 22, 2022
0.1.0 Feb 21, 2022

#945 in Math

Download history 108/week @ 2023-11-20 15/week @ 2023-11-27 23/week @ 2023-12-04 10/week @ 2023-12-11 16/week @ 2023-12-18 34/week @ 2023-12-25 35/week @ 2024-01-01 48/week @ 2024-01-08 39/week @ 2024-01-15 8/week @ 2024-01-22 9/week @ 2024-01-29 55/week @ 2024-02-05 207/week @ 2024-02-12 436/week @ 2024-02-19 1157/week @ 2024-02-26 1863/week @ 2024-03-04

3,675 downloads per month
Used in 9 crates (3 directly)

MIT/Apache

11KB
132 lines

fsum is the Rust library by Piotr Beling to calculate accurate sum of floats.

Example

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

Complexity

The complexities of summing n numbers are:

  • time: from O(n) (optimistic) to O(n²) (pessimistic)
  • memory: from O(1) (optimistic) to O(n) (pessimistic)

Usually the complexities are close to optimistic.

References

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

No runtime deps