util: add GeometricDecay util
This commit is contained in:
parent
04343c96d6
commit
80c7a1f47c
2 changed files with 42 additions and 0 deletions
41
src/utils/geometric_decay.rs
Normal file
41
src/utils/geometric_decay.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
use std::cell::Cell;
|
||||
|
||||
pub struct GeometricDecay {
|
||||
p1: f64,
|
||||
p2: f64,
|
||||
v: Cell<f64>,
|
||||
}
|
||||
|
||||
impl GeometricDecay {
|
||||
#[expect(dead_code)]
|
||||
pub fn new(mut p1: f64, reset: u64) -> Self {
|
||||
if p1.is_nan() || p1 < 0.01 {
|
||||
p1 = 0.01;
|
||||
}
|
||||
if p1 > 0.99 {
|
||||
p1 = 0.99;
|
||||
}
|
||||
let p2 = 1.0 - p1;
|
||||
Self {
|
||||
p1,
|
||||
p2,
|
||||
v: Cell::new(reset as f64 / p1),
|
||||
}
|
||||
}
|
||||
|
||||
#[expect(dead_code)]
|
||||
pub fn reset(&self, v: u64) {
|
||||
self.v.set(v as f64 / self.p1);
|
||||
}
|
||||
|
||||
#[expect(dead_code)]
|
||||
pub fn get(&self) -> u64 {
|
||||
(self.p1 * self.v.get()) as u64
|
||||
}
|
||||
|
||||
#[expect(dead_code)]
|
||||
pub fn add(&self, n: u64) {
|
||||
let v = n as f64 + self.p2 * self.v.get();
|
||||
self.v.set(v);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue