Crate dfkzr

Crate dfkzr 

Source
Expand description

§dfkzr - Dezimalfestkommazahlrechnung

dfkzr provides the type UDf64 which represents unsigned decimal fixed point numbers.

The number of integer and fractional digits is configurable with compile-time generic constants. Values are represented internally using u64. Arithmetic is done on u64 or u128, depending on the operation.

§Features

  • Basic unsigned fixed-point arithmetic
  • Wrapper types implementing arithmetic traits for convenience, see UDf64Checked and UDf64Saturating
  • up to 19 effective decimal digits (integer part and fractional combined)
  • #![no_std] compatible, no runtime dependencies except core (except for some tests)
  • No heap allocations
  • Written in straightforward safe Rust, accompanied by an extensive automated test suite
  • Display

§Examples

use dfkzr::UDf64;
assert_eq!(
    UDf64::<4, 2>::ONE.checked_add(UDf64::<4, 2>::ONE),
    Ok(UDf64::<4, 2>::try_from_parts(2, 0).unwrap())
);
use dfkzr::{UDf64, UDf64Checked};
let a: UDf64Checked<6, 3> = UDf64::try_from_parts(1_982, 198).unwrap().into();
let b: UDf64Checked<6, 3> = UDf64::try_from_parts(8_832, 932).unwrap().into();
let c: UDf64Checked<6, 3> = UDf64::try_from_parts(10_815, 130).unwrap().into();
assert_eq!(a + b, Ok(c));
use dfkzr::udf64;
assert_eq!(udf64!(07, 24).checked_add(udf64!(02, 95)), Ok(udf64!(10, 19)));
use dfkzr::{udf64, UDf64Saturating};
let a: UDf64Saturating<4, 4> = udf64!(1_483, 0349).into();
let b: UDf64Saturating<4, 4> = udf64!(0_053, 3820).into();
assert_eq!(a * b, UDf64Saturating::<4, 4>::MAX);
use dfkzr::udf64;
println!("{}", udf64!(0123, 000_789).display()); // prints "123.000789"

Macros§

udf64
Creates compile-time checked UDf64. Digit parameters are determined from the inputs.

Structs§

UDf64
unsigned decimal fixed point value based on u64
UDf64Checked
Unsigned decimal fixed point value based on u64 with checked arithmetic.
UDf64Saturating
Unsigned decimal fixed point value based on u64 with saturating arithmetic.

Enums§

ArithmeticError
Type representing an occured during arithmetic