pub struct Divisor<I: Int, S: Scale> { /* private fields */ }Expand description
A divisor prepared once, for dividing many values by it.
A book’s notionals by one price, or amounts by one rate: div_round and
checked_div_round take it where they take a decimal, and divide
by two multiplications and a comparison, where by a decimal they take a division. Preparing it
takes a division, so it pays where one divisor divides many values: on values of random lengths
by one divisor, a D64<8> quotient rounded half to even takes 3.00 ns prepared and 5.97 by the
decimal, and a D128<18> one 12.40 and 15.95, on a Graviton4
(bench/results/2026-10-14T06-08Z-pr18-baseline-graviton4).
§Examples
use ddust::round::HalfEven;
use ddust::{D64, Divisor, dec};
let price: D64<2> = dec!(64_250.50);
let by_price = Divisor::new(price).expect("not zero");
let notional: D64<8> = dec!(1_000_000);
assert_eq!(notional.div_round(by_price, HalfEven), dec!(15.56408121), "units of it");
assert_eq!(notional.div_round(by_price, HalfEven), notional.div_round(price, HalfEven));
assert!(Divisor::new(D64::<2>::ZERO).is_none(), "never zero");Implementations§
Source§impl<I: Int, S: Scale> Divisor<I, S>
impl<I: Int, S: Scale> Divisor<I, S>
Sourcepub const fn new(value: Decimal<I, S>) -> Option<Self>
pub const fn new(value: Decimal<I, S>) -> Option<Self>
value prepared as a divisor, or None for zero.
§Examples
use ddust::{D64, Divisor, dec};
let rate: D64<4> = dec!(1.0825);
assert_eq!(Divisor::new(rate).map(Divisor::get), Some(rate), "prepared");
assert!(Divisor::new(D64::<4>::ZERO).is_none(), "never zero");Trait Implementations§
impl<I: Copy + Int, S: Copy + Scale> Copy for Divisor<I, S>where
I::Prepared: Copy,
impl<I: Int, T: Scale> DivisorOf<I> for Divisor<I, T>
impl<I: Int, S: Scale> Eq for Divisor<I, S>
Auto Trait Implementations§
impl<I, S> Freeze for Divisor<I, S>
impl<I, S> RefUnwindSafe for Divisor<I, S>
impl<I, S> Send for Divisor<I, S>
impl<I, S> Sync for Divisor<I, S>
impl<I, S> Unpin for Divisor<I, S>
impl<I, S> UnsafeUnpin for Divisor<I, S>
impl<I, S> UnwindSafe for Divisor<I, S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more