Skip to main content

Divisor

Struct Divisor 

Source
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>

Source

pub const fn new(value: Decimal<I, S>) -> Option<Self>
where I: Int, S: Scale,

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");
Source

pub const fn get(self) -> Decimal<I, S>

The value it divides by.

Trait Implementations§

Source§

impl<I: Clone + Int, S: Clone + Scale> Clone for Divisor<I, S>
where I::Prepared: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<I: Copy + Int, S: Copy + Scale> Copy for Divisor<I, S>
where I::Prepared: Copy,

Source§

impl<I: Debug + Int, S: Debug + Scale> Debug for Divisor<I, S>
where I::Prepared: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<I: Int, T: Scale> DivisorOf<I> for Divisor<I, T>

Source§

impl<I: Int, S: Scale> Eq for Divisor<I, S>

Source§

impl<I: Int, S: Scale> Hash for Divisor<I, S>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<I: Int, S: Scale> PartialEq for Divisor<I, S>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<I, S> Freeze for Divisor<I, S>
where Decimal<I, S>: Freeze, <I as Int>::Prepared: Freeze,

§

impl<I, S> RefUnwindSafe for Divisor<I, S>
where Decimal<I, S>: RefUnwindSafe, <I as Int>::Prepared: RefUnwindSafe,

§

impl<I, S> Send for Divisor<I, S>
where Decimal<I, S>: Send, <I as Int>::Prepared: Send,

§

impl<I, S> Sync for Divisor<I, S>
where Decimal<I, S>: Sync, <I as Int>::Prepared: Sync,

§

impl<I, S> Unpin for Divisor<I, S>
where Decimal<I, S>: Unpin, <I as Int>::Prepared: Unpin,

§

impl<I, S> UnsafeUnpin for Divisor<I, S>
where Decimal<I, S>: UnsafeUnpin, <I as Int>::Prepared: UnsafeUnpin,

§

impl<I, S> UnwindSafe for Divisor<I, S>
where Decimal<I, S>: UnwindSafe, <I as Int>::Prepared: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.