//! Shared mathematical utilities.

use rust_decimal::Decimal;

/// Square root approximation using Newton-Raphson.
pub(crate) fn decimal_sqrt(x: Decimal) -> Decimal {
    if x <= Decimal::ZERO {
        return Decimal::ZERO;
    }

    let mut guess = x / Decimal::from(2);
    let tolerance = Decimal::new(1, 10);

    for _ in 0..50 {
        let next = (guess + x / guess) / Decimal::from(2);
        if (next - guess).abs() < tolerance {
            return next;
        }
        guess = next;
    }

    guess
}

/// Calculate mean of values.
pub(crate) fn mean(values: &[Decimal]) -> Decimal {
    if values.is_empty() {
        return Decimal::ZERO;
    }
    let sum: Decimal = values.iter().sum();
    sum / Decimal::from(values.len() as u64)
}

/// Calculate sample standard deviation.
pub(crate) fn std_deviation(values: &[Decimal]) -> Decimal {
    if values.len() < 2 {
        return Decimal::ZERO;
    }

    let m = mean(values);
    let variance: Decimal = values.iter().map(|&x| (x - m) * (x - m)).sum();
    let variance = variance / Decimal::from((values.len() - 1) as u64);

    decimal_sqrt(variance)
}

#[cfg(test)]
#[path = "math_tests.rs"]
mod tests;
