chapaty 1.3.1

An event-driven Rust engine for building and evaluating quantitative trading agents. Features a Gym-style API for algorithmic backtesting and reinforcement learning.
Documentation
use serde::{Deserialize, Serialize};

/// A precise floating-point accumulator using the [Kahan Summation Algorithm](https://en.wikipedia.org/wiki/Kahan_summation_algorithm).
/// Implemented using move semantics for functional, immutable state
/// transitions.
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub struct KahanSum {
    sum: f64,
    c: f64,
}

impl KahanSum {
    /// Consumes the current state and yields a new [`KahanSum`] containing the
    /// updated running totals.
    pub fn add(self, value: f64) -> Self {
        let y = value - self.c;
        let t = self.sum + y;
        let c = (t - self.sum) - y;

        Self { sum: t, c }
    }

    /// Returns the current mathematically compensated sum.
    pub const fn value(self) -> f64 {
        self.sum
    }
}