truecalc-core 3.2.0

Formula engine with exact Google Sheets semantics — stateless, embeddable evaluator
Documentation
use crate::eval::coercion::to_number;
use crate::eval::functions::check_arity;
use crate::types::{ErrorKind, Value};

const MAX_BIT: u64 = 281_474_976_710_655; // 2^48 - 1

pub fn bitrshift_fn(args: &[Value]) -> Value {
    if let Some(err) = check_arity(args, 2, 2) {
        return err;
    }
    let number = match to_number(args[0].clone()) {
        Ok(n) => n,
        Err(e) => return e,
    };
    let shift = match to_number(args[1].clone()) {
        Ok(n) => n,
        Err(e) => return e,
    };
    if number < 0.0 || number > MAX_BIT as f64 || number.fract() != 0.0 {
        return Value::Error(ErrorKind::Num);
    }
    let num = number as u64;
    let shift_amount = shift.trunc() as i64;
    // GS: |shift| > 53 returns #NUM!
    if shift_amount.abs() > 53 {
        return Value::Error(ErrorKind::Num);
    }
    let result = if shift_amount >= 0 {
        let s = shift_amount as u32;
        num >> s
    } else {
        let s = (-shift_amount) as u32;
        num.checked_shl(s).unwrap_or(u64::MAX)
    };
    if result > MAX_BIT {
        return Value::Error(ErrorKind::Num);
    }
    Value::Number(result as f64)
}

#[cfg(test)]
mod tests;