Shared Rust kernels

ported/dollar_fraction_family

All modules · quoted at commit 9e1884369a67 of DnaCalc/DnaCalc. Comments are the source's own.

normalized_fraction_denominator · 12 lines · calc-func/src/ported/dollar_fraction_family.rs on GitHub
/// The fraction argument truncated to an integer, kept as a double: an `i32` saturates at
/// 2^31-1 and made `DOLLARDE(1.5, 1e10)` 3.328 where Excel gives 1.5.
fn normalized_fraction_denominator(fraction: f64) -> Result<f64, WorksheetErrorCode> {
    if fraction < 0.0 {
        return Err(WorksheetErrorCode::Num);
    }
    let truncated = fraction.trunc();
    if truncated == 0.0 {
        return Err(WorksheetErrorCode::Div0);
    }
    Ok(truncated)
}

Used by 2 functions.

decimal_scale · 12 lines · calc-func/src/ported/dollar_fraction_family.rs on GitHub
/// The smallest power of ten that is at least the denominator: 10^ceil(log10(d)). Excel uses
/// this, not 10^(digit count): the two differ exactly when d is a power of ten
/// (`DOLLARDE(3.69, 10)` is 3.69, not 9.9; `DOLLARDE(x, 1)` is x). Live Excel 20430, W111-5
/// G8-01.
fn decimal_scale(denominator: f64) -> f64 {
    let d = denominator.abs();
    let mut scale = 1.0;
    while scale < d {
        scale *= 10.0;
    }
    scale
}

Used by 2 functions.