Generic Moving Average calculation for the integer types.
and float types
Add this to your Cargo.toml:
[dependencies]
movavg = "2"
// Integers
let mut avg: MovAvg<i32, i32, 3> = MovAvg::new(); // window size = 3
assert_eq!(avg.feed(10), 10);
assert_eq!(avg.feed(20), 15);
assert_eq!(avg.feed(30), 20);
assert_eq!(avg.feed(40), 30);
assert_eq!(avg.get(), 30);
// Floats
let mut avg: MovAvg<f64, f64, 3> = MovAvg::new();
assert_eq!(avg.feed(10.0), 10.0);
assert_eq!(avg.feed(20.0), 15.0);
assert_eq!(avg.feed(30.0), 20.0);
assert_eq!(avg.feed(40.0), 30.0);
assert_eq!(avg.get(), 30.0);
// Bigger accumulator
let mut avg: MovAvg<i8, i32, 3> = MovAvg::new();
assert_eq!(avg.feed(100), 100);
assert_eq!(avg.feed(100), 100); // This would overflow an i8 accumulator
If you want to use movavg without the std
library (often
called no_std
), then use the following Cargo.toml
dependency to disable the std
feature:
[dependencies]
movavg = { version = "2", default-features = false }
Currently the no_std
variant supports all functionality
that the default std
variant supports. But that may change
in future.
The fastfloat
feature can be used to enable much faster,
but less accurate floating point calculations. Enabling this feature
leads to bigger floating point rounding and cancellation errors.
[dependencies]
movavg = { version = "2", features = ["fastfloat"] }
This feature may also be used together with disabled std
feature (see no_std
).
Requires Rust compiler version 1.61 or later.
Copyright (c) 2021-2025 Michael Büsch m@bues.ch
Licensed under the Apache License version 2.0 or the MIT license, at your option.