pub trait Accumulator: Copy + PartialOrd + PartialEq + Into<f64> {
const ZERO: Self;
const ONE: Self;
fn acc_in(v: f64) -> Self;
fn acc_add(self, other: Self) -> Self;
fn acc_mul(self, other: Self) -> Self;
fn acc_div(self, n: usize) -> Self;
}
impl Accumulator for f32 {
const ZERO: Self = 0.0;
const ONE: Self = 1.0;
#[inline]
fn acc_in(v: f64) -> Self {
v as f32
}
#[inline]
fn acc_add(self, other: Self) -> Self {
self + other
}
#[inline]
fn acc_mul(self, other: Self) -> Self {
self * other
}
#[inline]
fn acc_div(self, n: usize) -> Self {
self / n as f32
}
}
impl Accumulator for f64 {
const ZERO: Self = 0.0;
const ONE: Self = 1.0;
#[inline]
fn acc_in(v: f64) -> Self {
v
}
#[inline]
fn acc_add(self, other: Self) -> Self {
self + other
}
#[inline]
fn acc_mul(self, other: Self) -> Self {
self * other
}
#[inline]
fn acc_div(self, n: usize) -> Self {
self / n as f64
}
}