fpdec-core 0.13.2

Common constants and functions for crate fpdec.
Documentation
// ---------------------------------------------------------------------------
// Copyright:   (c) 2021 ff. Michael Amrhein (michael@adrhinum.de)
// License:     This program is part of a larger application. For license
//              details please read the file LICENSE.TXT provided together
//              with the application.
// ---------------------------------------------------------------------------
// $Source: fpdec-core/src/powers_of_ten.rs $
// $Revision: 2023-05-12T15:46:50+02:00 $

const POWERS_OF_10: [i128; 39] = [
    1,
    10,
    100,
    1_000,
    10_000,
    100_000,
    1_000_000,
    10_000_000,
    100_000_000,
    1_000_000_000,
    10_000_000_000,
    100_000_000_000,
    1_000_000_000_000,
    10_000_000_000_000,
    100_000_000_000_000,
    1_000_000_000_000_000,
    10_000_000_000_000_000,
    100_000_000_000_000_000,
    1_000_000_000_000_000_000,
    10_000_000_000_000_000_000,
    100_000_000_000_000_000_000,
    1_000_000_000_000_000_000_000,
    10_000_000_000_000_000_000_000,
    100_000_000_000_000_000_000_000,
    1_000_000_000_000_000_000_000_000,
    10_000_000_000_000_000_000_000_000,
    100_000_000_000_000_000_000_000_000,
    1_000_000_000_000_000_000_000_000_000,
    10_000_000_000_000_000_000_000_000_000,
    100_000_000_000_000_000_000_000_000_000,
    1_000_000_000_000_000_000_000_000_000_000,
    10_000_000_000_000_000_000_000_000_000_000,
    100_000_000_000_000_000_000_000_000_000_000,
    1_000_000_000_000_000_000_000_000_000_000_000,
    10_000_000_000_000_000_000_000_000_000_000_000,
    100_000_000_000_000_000_000_000_000_000_000_000,
    1_000_000_000_000_000_000_000_000_000_000_000_000,
    10_000_000_000_000_000_000_000_000_000_000_000_000,
    100_000_000_000_000_000_000_000_000_000_000_000_000,
];

#[doc(hidden)]
#[inline(always)]
#[must_use]
pub const fn ten_pow(n: u8) -> i128 {
    POWERS_OF_10[n as usize]
}

#[doc(hidden)]
#[inline(always)]
pub const fn checked_ten_pow(n: u8) -> Option<i128> {
    if n > 38 {
        None
    } else {
        Some(POWERS_OF_10[n as usize])
    }
}

#[doc(hidden)]
#[inline(always)]
#[must_use]
pub const fn mul_pow_ten(val: i128, n: u8) -> i128 {
    val * ten_pow(n)
}

#[doc(hidden)]
#[inline(always)]
#[must_use]
pub fn checked_mul_pow_ten(val: i128, n: u8) -> Option<i128> {
    val.checked_mul(checked_ten_pow(n)?)
}