FSum

Struct FSum 

Source
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

Source

pub fn new() -> FSum

Constructs zeroed accumulator.

§Example
use fsum::FSum;

assert_eq!(FSum::new().value(), 0.0);
Source

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

pub fn with_all<Bf64, InIter>(values: InIter) -> FSum
where Bf64: Borrow<f64>, InIter: IntoIterator<Item = Bf64>,

Constructs accumulator with all values from iter.

§Examples
use fsum::FSum;

assert_eq!(FSum::with_all((0..10).map(|_| 0.1)).value(), 1.0);
Source

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.

Source

pub fn add_all<InIter, Bf64>(&mut self, values: InIter) -> &mut FSum
where Bf64: Borrow<f64>, InIter: IntoIterator<Item = Bf64>,

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

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

pub fn reset(&mut self) -> &mut FSum

Sets the current sum to 0 and returns self.

Source

pub fn set(&mut self, value: f64) -> &mut FSum

Sets the current sum to value and returns self.

§Example
use fsum::FSum;

assert_eq!(FSum::new().set(1.0).value(), 1.0);

Trait Implementations§

Source§

impl AddAssign<f64> for FSum

Source§

fn add_assign(&mut self, other: f64)

Performs the += operation. Read more
Source§

impl Clone for FSum

Source§

fn clone(&self) -> FSum

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FSum

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for FSum

Source§

fn default() -> FSum

Returns the “default value” for a type. Read more
Source§

impl From<&FSum> for f64

Source§

fn from(fsum: &FSum) -> Self

Converts to this type from the input type.
Source§

impl From<FSum> for f64

Source§

fn from(fsum: FSum) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for FSum

Source§

fn from(initial_value: f64) -> Self

Converts to this type from the input type.
Source§

impl SubAssign<f64> for FSum

Source§

fn sub_assign(&mut self, other: f64)

Performs the -= operation. Read more

Auto 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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.