pub struct NeumaierSumReal<T: NumKernel> { /* private fields */ }Expand description
Neumaier compensated sum of an iterable object of floating-point numbers.
When summing floating-point values in a vector, the standard method of adding values sequentially can lead to precision loss, especially when there are a large number of elements or a mix of very large and very small values. This happens because floating-point arithmetic is not associative due to rounding errors. The most accurate algorithm to sum floating-point numbers typically avoids these precision problems.
Kahan summation is a popular algorithm that reduces numerical errors when adding a sequence of floating-point numbers. It keeps track of a running compensation for lost low-order bits during summation.
Neumaier summation is a slight modification of Kahan summation that can handle larger errors better. It uses an extra step to correct for the compensation term if the summation results in a larger round-off error than Kahan’s method can correct.
§Example
use ftl_numkernel::{
Native64,
neumaier_compensated_sum::{NeumaierSum,NeumaierSumReal}
};
let mut neumaier = NeumaierSumReal::<Native64>::zero();
neumaier.add(1.0);
neumaier.add(1e100);
neumaier.add(1.0);
neumaier.add(-1e100);
let sum = neumaier.sum();
println!("Sum: {}", sum);
assert_eq!(sum, 2.0);§References
Trait Implementations§
Source§impl<T: Clone + NumKernel> Clone for NeumaierSumReal<T>
impl<T: Clone + NumKernel> Clone for NeumaierSumReal<T>
Source§fn clone(&self) -> NeumaierSumReal<T>
fn clone(&self) -> NeumaierSumReal<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: NumKernel> NeumaierSum<T> for NeumaierSumReal<T>
impl<T: NumKernel> NeumaierSum<T> for NeumaierSumReal<T>
Source§fn new(value: T::RealType) -> Self
fn new(value: T::RealType) -> Self
Creates a new instance of the Neumaier compensated sum with user provided initial value.
This is the default constructor.
Source§fn add(&mut self, value: T::RealType)
fn add(&mut self, value: T::RealType)
Adds a value to the sum. This method should be called for each value to be summed.
type ScalarType = <T as NumKernel>::RealType
Source§fn zero() -> Self
fn zero() -> Self
Source§fn new_sequential<I>(values: I) -> Selfwhere
I: IntoIterator<Item = Self::ScalarType>,
fn new_sequential<I>(values: I) -> Selfwhere
I: IntoIterator<Item = Self::ScalarType>,
NeumaierSumReal object summing the values in the iterable object values (sequential_version).
/// # Example Read more