Skip to main content

RunningAverage

Struct RunningAverage 

Source
pub struct RunningAverage<T: Averageable> { /* private fields */ }
Expand description

Running Average (FB_RunningAverage)

Accumulates values and computes their arithmetic mean. Stores only the sum and count internally — no heap allocation.

Generic over any numeric type that implements Averageable (all standard primitives: f32, f64, i32, u64, etc.).

§Example

use autocore_std::fb::RunningAverage;

let mut avg = RunningAverage::<f64>::new();

avg.append(10.0);
avg.append(20.0);
avg.append(30.0);

assert_eq!(avg.count(), 3);
assert!((avg.average() - 20.0).abs() < f64::EPSILON);

avg.reset();
assert_eq!(avg.count(), 0);
assert_eq!(avg.average(), 0.0);

§Use Cases

  • Smoothing noisy sensor readings
  • Computing mean cycle times
  • Averaging analog input samples over N scans

Implementations§

Source§

impl<T: Averageable> RunningAverage<T>

Source

pub fn new() -> Self

Creates a new running average with zero samples.

§Example
use autocore_std::fb::RunningAverage;

let avg = RunningAverage::<f64>::new();
assert_eq!(avg.count(), 0);
assert_eq!(avg.average(), 0.0);
Source

pub fn append(&mut self, value: T)

Appends a value to the running average.

§Arguments
  • value - The value to include in the average
§Example
use autocore_std::fb::RunningAverage;

let mut avg = RunningAverage::<f64>::new();
avg.append(5.0);
avg.append(15.0);
assert_eq!(avg.count(), 2);
assert!((avg.average() - 10.0).abs() < f64::EPSILON);
Source

pub fn reset(&mut self)

Resets the running average, clearing all accumulated data.

§Example
use autocore_std::fb::RunningAverage;

let mut avg = RunningAverage::<f64>::new();
avg.append(42.0);
assert_eq!(avg.count(), 1);

avg.reset();
assert_eq!(avg.count(), 0);
assert_eq!(avg.average(), 0.0);
Source

pub fn average(&self) -> T

Returns the arithmetic mean of all appended values.

Returns the default value for T (zero) when no values have been appended. For integer types, the result is truncated (integer division).

§Example
use autocore_std::fb::RunningAverage;

let mut avg = RunningAverage::<f64>::new();
assert_eq!(avg.average(), 0.0); // No values yet

avg.append(10.0);
avg.append(20.0);
assert!((avg.average() - 15.0).abs() < f64::EPSILON);
Source

pub fn count(&self) -> usize

Returns the number of values appended since creation or the last reset.

§Example
use autocore_std::fb::RunningAverage;

let mut avg = RunningAverage::<f32>::new();
assert_eq!(avg.count(), 0);

avg.append(1.0);
avg.append(2.0);
assert_eq!(avg.count(), 2);

Trait Implementations§

Source§

impl<T: Clone + Averageable> Clone for RunningAverage<T>

Source§

fn clone(&self) -> RunningAverage<T>

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<T: Debug + Averageable> Debug for RunningAverage<T>

Source§

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

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

impl<T: Averageable> Default for RunningAverage<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> Freeze for RunningAverage<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for RunningAverage<T>
where T: RefUnwindSafe,

§

impl<T> Send for RunningAverage<T>
where T: Send,

§

impl<T> Sync for RunningAverage<T>
where T: Sync,

§

impl<T> Unpin for RunningAverage<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for RunningAverage<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for RunningAverage<T>
where T: UnwindSafe,

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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V