pub struct FSum { /* private fields */ }
Expand description
Accumulator that represents the exact sum of f64
values and
allows additional f64
values to be added without loss of precision.
Implementations§
Source§impl FSum
impl FSum
Sourcepub fn with_value(initial_value: f64) -> FSum
pub fn with_value(initial_value: f64) -> FSum
Constructs accumulator with given initial_value
.
§Example
use fsum::FSum;
assert_eq!(FSum::with_value(5.0).value(), 5.0);
Sourcepub fn with_all<Bf64, InIter>(values: InIter) -> FSum
pub fn with_all<Bf64, InIter>(values: InIter) -> FSum
Constructs accumulator with all values from iter
.
§Examples
use fsum::FSum;
assert_eq!(FSum::with_all((0..10).map(|_| 0.1)).value(), 1.0);
Sourcepub fn add(&mut self, x: f64) -> &mut FSum
pub fn add(&mut self, x: f64) -> &mut FSum
Increases the sum by x
and returns self
.
§Example
use fsum::FSum;
let mut s = FSum::new();
assert_eq!(s.value(), 0.0);
s.add(1.0);
assert_eq!(s.value(), 1.0);
s.add(2.0);
assert_eq!(s.value(), 3.0);
assert_eq!(s.add(5.0).value(), 8.0);
§Complexity
The complexities are:
- time: from O(1) (optimistic) to O(n) (pessimistic), where n is the number of values added so far,
- memory: O(1), but internal vector stored in
self
can be increased by 1 element.
Usually the time complexity is close to optimistic.
Sourcepub fn add_all<InIter, Bf64>(&mut self, values: InIter) -> &mut FSum
pub fn add_all<InIter, Bf64>(&mut self, values: InIter) -> &mut FSum
Increases the sum by all values from iter
. Returns self
.
§Example
use fsum::FSum;
assert_eq!(FSum::new().add_all((0..10).map(|_| 0.1)).value(), 1.0);
Sourcepub fn value(&self) -> f64
pub fn value(&self) -> f64
Returns the current value of the sum.
The complexities are:
- time: from O(1) (optimistic) to O(n) (pessimistic), where n is the number of values added so far,
- memory: O(1).
Usually the time complexity is close to optimistic.
§Example
use fsum::FSum;
assert_eq!(FSum::with_value(2.0).value(), 2.0);
Trait Implementations§
Source§impl AddAssign<f64> for FSum
impl AddAssign<f64> for FSum
Source§fn add_assign(&mut self, other: f64)
fn add_assign(&mut self, other: f64)
Performs the
+=
operation. Read moreAuto Trait Implementations§
impl Freeze for FSum
impl RefUnwindSafe for FSum
impl Send for FSum
impl Sync for FSum
impl Unpin for FSum
impl UnwindSafe for FSum
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