Expand description
§Decimal32
Decimal32 is a transparent wrapper over an i32. A const generic declares the number of places after the decimal point.
The motivation for such a type is providing lossless arithmetic guarantees like in the example below.
use high_roller::decimal::D9;
use num_traits::{CheckedAdd, WrappingAdd, WrappingSub};
const SMALL: f64 = 0.111000111;
const LARGE: f64 = 2.147483647;
const CHECKED_SMALL: D9 = D9::checked(SMALL).unwrap();
const CHECKED_LARGE: D9 = D9::checked(LARGE).unwrap();
// Parity with lossless operations
let sum = const { D9::checked(1.).unwrap() }.checked_add(&CHECKED_SMALL);
assert_eq!(sum.unwrap().get(), 1. + SMALL, "Result fits in f64");
// Checked operations prevent overflow
let lossy = CHECKED_LARGE.checked_add(&CHECKED_SMALL);
assert_eq!(lossy, None, "Result overflows i32");
assert_ne!(LARGE + SMALL - LARGE, SMALL);
// Wrapping operations enable loss recovery
let wrapped = CHECKED_LARGE.wrapping_add(&CHECKED_SMALL);
assert_eq!(wrapped.wrapping_sub(&CHECKED_LARGE), CHECKED_SMALL);Structs§
- Decimal32
- A 32-bit lossless number with const-defined decimal precision.
Enums§
- Decimal
Err - Enumerates the possible errors
Decimaloperations may return.
Type Aliases§
- D1
- A
Decimal32type with one significant figure after the decimal point. - D2
- A
Decimal32type with two significant figures after the decimal point. - D3
- A
Decimal32type with three significant figures after the decimal point. - D4
- A
Decimal32type with four significant figures after the decimal point. - D5
- A
Decimal32type with five significant figures after the decimal point. - D6
- A
Decimal32type with six significant figures after the decimal point. - D7
- A
Decimal32type with seven significant figures after the decimal point. - D8
- A
Decimal32type with eight significant figures after the decimal point. - D9
- A
Decimal32type with nine significant figures after the decimal point.