truecalc-core 3.2.0

Formula engine with exact Google Sheets semantics — stateless, embeddable evaluator
Documentation
use crate::types::{ErrorKind, Value};
use super::stat_helpers::collect_nums_direct;

/// `AVEDEV(value1, ...)` — average of absolute deviations from the mean.
/// Direct bool/text-number args coerce; non-numeric text → #VALUE!.
/// Returns `#DIV/0!` if no numeric values.
pub fn avedev_fn(args: &[Value]) -> Value {
    if args.is_empty() {
        return Value::Error(ErrorKind::NA);
    }
    let nums = match collect_nums_direct(args) {
        Ok(v) => v,
        Err(e) => return e,
    };
    let n = nums.len();
    if n == 0 {
        return Value::Error(ErrorKind::DivByZero);
    }
    let mean = nums.iter().sum::<f64>() / n as f64;
    let avedev = nums.iter().map(|x| (x - mean).abs()).sum::<f64>() / n as f64;
    if !avedev.is_finite() {
        return Value::Error(ErrorKind::Num);
    }
    Value::Number(avedev)
}

#[cfg(test)]
mod tests;