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>
impl<T: Averageable> RunningAverage<T>
Sourcepub fn new() -> Self
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);Sourcepub fn append(&mut self, value: T)
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);Sourcepub fn reset(&mut self)
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);Sourcepub fn average(&self) -> T
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);Trait Implementations§
Source§impl<T: Clone + Averageable> Clone for RunningAverage<T>
impl<T: Clone + Averageable> Clone for RunningAverage<T>
Source§fn clone(&self) -> RunningAverage<T>
fn clone(&self) -> RunningAverage<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T: Debug + Averageable> Debug for RunningAverage<T>
impl<T: Debug + Averageable> Debug for RunningAverage<T>
Source§impl<T: Averageable> Default for RunningAverage<T>
impl<T: Averageable> Default for RunningAverage<T>
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> 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