pub struct KahanSum<T: Float> { /* private fields */ }Expand description
Kahan compensated summation register.
This is a register that can be used to sum a sequence of floating point numbers with a better precision than a naive summation.
See https://en.wikipedia.org/wiki/Kahan_summation_algorithm
Examples
ⓘ
let repetitions = 10_000;
let mut naive = 0.0_f32;
let mut sum = KahanSum::new(0.0_f32);
(1..=repetitions).for_each(|_| {
sum += 0.1;
naive += 0.1;
});
assert_eq!(sum.sum(), repetitions as f32 * 0.1);
assert_ne!(naive, repetitions as f32 * 0.1);Implementations§
Trait Implementations§
source§impl<T: Float> AddAssign<KahanSum<T>> for KahanSum<T>
impl<T: Float> AddAssign<KahanSum<T>> for KahanSum<T>
source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Performs the
+= operation. Read moresource§impl<T: Float> AddAssign<T> for KahanSum<T>
impl<T: Float> AddAssign<T> for KahanSum<T>
source§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
Performs the
+= operation. Read moresource§impl<T: Float> PartialEq<KahanSum<T>> for KahanSum<T>
impl<T: Float> PartialEq<KahanSum<T>> for KahanSum<T>
impl<T: Copy + Float> Copy for KahanSum<T>
Auto Trait Implementations§
impl<T> RefUnwindSafe for KahanSum<T>where T: RefUnwindSafe,
impl<T> Send for KahanSum<T>where T: Send,
impl<T> Sync for KahanSum<T>where T: Sync,
impl<T> Unpin for KahanSum<T>where T: Unpin,
impl<T> UnwindSafe for KahanSum<T>where T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.