Skip to main content

Divisor

Struct Divisor 

Source
pub struct Divisor { /* private fields */ }
Expand description

A number prepared to divide others by: dividing by it gives what dividing by the number does, with the same operators, but skips reading the divisor and computing its reciprocal each time.

It keeps the magnitude of the number in limbs of eight bytes, shifted so that the top bit is set, the reciprocal of the top limbs and the sign; and from its first division by Newton’s method, from 256 limbs (2 KiB) in the divisor and in the quotient, the inverse of all its limbs and the transforms of the inverse and of the divisor, 10 to 11 times the divisor’s bytes in all, which that first division takes about a third longer to compute than a division by the number, and which take the later ones to about half its time. A number divided by it with /, /=, % or %=, owned or borrowed, or by its methods div_rem, div_euclid and rem_euclid, gives the results that dividing by the number gives, in the byte order of the dividend, in the shortest form, built in the dividend’s vector when the dividend is owned (which always has room, but for zero in an empty vector) and otherwise in one new vector of exactly their length.

§Examples

use a::math::numbers::{ByteOrder, Divisor, Number};

// Dividing many numbers by 10^19.
let d = Divisor::new(&Number::new_from_u64(10_000_000_000_000_000_000, ByteOrder::BigEndian));
let a = Number::new_from_u128(123 * 10_000_000_000_000_000_000 + 45, ByteOrder::LittleEndian);
assert_eq!((&a / &d).value, [123]);
assert_eq!((&a % &d).value, [45]);

let (q, r) = d.div_rem(&Number::new_from_i32(-7, ByteOrder::BigEndian));
assert!(q.value == [0] && r.value == [7] && r.is_negative());

Implementations§

Source§

impl Divisor

Source

pub fn new(d: &Number) -> Divisor

Prepares d for dividing by it: reads its magnitude into limbs, shifted, and computes the reciprocal of the top ones. d may have any sign and byte order, and need not be in the shortest form.

Takes O(n) time for n bytes, and allocates one vector of their limbs.

§Panics

When d is zero, as dividing by it would.

§Examples
use a::math::numbers::{ByteOrder, Divisor, Number};

let d = Divisor::new(&Number::new_from_i32(-3, ByteOrder::BigEndian));
assert!(d.is_negative());
let q = &Number::new_from_i32(100, ByteOrder::LittleEndian) / &d;
assert_eq!(q.value, [33]);
assert!(q.is_negative());
Source

pub fn is_negative(&self) -> bool

Returns whether the divisor is below zero.

Source

pub fn div_rem(&self, a: &Number) -> (Number, Number)

Returns the quotient and the remainder of a divided by this divisor, (a / d, a % d), computed together, as Number::div_rem gives them: the quotient truncated toward zero, the remainder with the sign of a, both in the byte order of a and in the shortest form, each in one new vector of exactly its length.

§Examples
use a::math::numbers::{ByteOrder, Divisor, Number};

let d = Divisor::new(&Number::new_from_u16(1000, ByteOrder::BigEndian));
let (q, r) = d.div_rem(&Number::new_from_u32(1_234_567, ByteOrder::BigEndian));
assert_eq!(q.value, [0x04, 0xD2]);
assert_eq!(r.value, [0x02, 0x37]);
Source

pub fn div_euclid(&self, a: &Number) -> Number

Returns the quotient of Euclid’s division of a by this divisor, as Number::div_euclid gives it, in one new vector.

§Examples
use a::math::numbers::{ByteOrder, Divisor, Number};

let d = Divisor::new(&Number::new_from_i32(2, ByteOrder::BigEndian));
let q = d.div_euclid(&Number::new_from_i32(-7, ByteOrder::BigEndian));
assert_eq!(q.value, [4]);
assert!(q.is_negative());
Source

pub fn rem_euclid(&self, a: &Number) -> Number

Returns the remainder of Euclid’s division of a by this divisor, a modulo |d|, as Number::rem_euclid gives it, never negative, in one new vector.

§Examples
use a::math::numbers::{ByteOrder, Divisor, Number};

let d = Divisor::new(&Number::new_from_i32(-2, ByteOrder::BigEndian));
let r = d.rem_euclid(&Number::new_from_i32(-7, ByteOrder::BigEndian));
assert_eq!(r.value, [1]);
assert!(!r.is_negative());
Source

pub fn div_floor(&self, a: &Number) -> Number

Returns the quotient of a divided by this divisor rounded toward negative infinity, as Number::div_floor gives it, in the byte order of a and in the shortest form.

§Examples
use a::math::numbers::{Divisor, Number};

let d = Divisor::new(&Number::from(-2));
assert_eq!(d.div_floor(&Number::from(7)), -4);
assert_eq!(d.div_floor(&Number::from(-7)), 3);
Source

pub fn rem_floor(&self, a: &Number) -> Number

Returns the remainder of a divided by this divisor rounded toward negative infinity, as Number::rem_floor gives it: it has the sign of the divisor, unless it is zero.

§Examples
use a::math::numbers::{Divisor, Number};

let d = Divisor::new(&Number::from(-2));
assert_eq!(d.rem_floor(&Number::from(7)), -1);
assert_eq!(d.rem_floor(&Number::from(-7)), -1);

Trait Implementations§

Source§

impl Clone for Divisor

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 Div<&Divisor> for &Number

Source§

fn div(self, d: &Divisor) -> Number

Returns self / d, truncated toward zero, as dividing by the number d was made from does, in one new vector of exactly its length.

§Examples
use a::math::numbers::{ByteOrder, Divisor, Number};

let d = Divisor::new(&Number::new_from_u8(7, ByteOrder::BigEndian));
let q = &Number::new_from_i32(-100, ByteOrder::LittleEndian) / &d;
assert_eq!(q.value, [14]);
assert!(q.is_negative());
Source§

type Output = Number

The resulting type after applying the / operator.
Source§

impl Div<&Divisor> for Number

Source§

fn div(self, d: &Divisor) -> Number

Returns self / d, as self /= d does, in the vector of self.

§Examples
use a::math::numbers::{ByteOrder, Divisor, Number};

let d = Divisor::new(&Number::new_from_u8(16, ByteOrder::BigEndian));
let a = Number::new_from_u32(0x1234_5678, ByteOrder::BigEndian);
let pointer = a.value.as_ptr();
let q = a / &d;
assert_eq!(q.value, [0x01, 0x23, 0x45, 0x67]);
assert_eq!(q.value.as_ptr(), pointer);
Source§

type Output = Number

The resulting type after applying the / operator.
Source§

impl DivAssign<&Divisor> for Number

Source§

fn div_assign(&mut self, d: &Divisor)

Divides self by d, truncating toward zero, keeping the byte order of self, in its vector, which has room for the quotient unless it is empty.

§Examples
use a::math::numbers::{ByteOrder, Divisor, Number};

let ten = Divisor::new(&Number::new_from_u8(10, ByteOrder::BigEndian));
let mut n = Number::new_from_u32(12_345, ByteOrder::LittleEndian);
n /= &ten;
n /= &ten;
assert_eq!(n.value, [123]);
Source§

impl Rem<&Divisor> for &Number

Source§

fn rem(self, d: &Divisor) -> Number

Returns self % d, with the sign of self, as the remainder by the number d was made from, in one new vector of exactly its length.

§Examples
use a::math::numbers::{ByteOrder, Divisor, Number};

let d = Divisor::new(&Number::new_from_i8(-7, ByteOrder::BigEndian));
let r = &Number::new_from_i32(100, ByteOrder::LittleEndian) % &d;
assert_eq!(r.value, [2]);
assert!(!r.is_negative());
Source§

type Output = Number

The resulting type after applying the % operator.
Source§

impl Rem<&Divisor> for Number

Source§

fn rem(self, d: &Divisor) -> Number

Returns self % d, as self %= d does, in the vector of self.

§Examples
use a::math::numbers::{ByteOrder, Divisor, Number};

let d = Divisor::new(&Number::new_from_u16(1000, ByteOrder::BigEndian));
let r = Number::new_from_u32(1_234_567, ByteOrder::BigEndian) % &d;
assert_eq!(r.value, [0x02, 0x37]);
Source§

type Output = Number

The resulting type after applying the % operator.
Source§

impl RemAssign<&Divisor> for Number

Source§

fn rem_assign(&mut self, d: &Divisor)

Sets self to self % d, keeping the byte order of self, in its vector, which has room for the remainder unless it is empty.

§Examples
use a::math::numbers::{ByteOrder, Divisor, Number};

let d = Divisor::new(&Number::new_from_u8(9, ByteOrder::BigEndian));
let mut n = Number::new_from_i32(-100, ByteOrder::LittleEndian);
n %= &d;
assert_eq!(n.value, [1]);
assert!(n.is_negative());

Auto Trait Implementations§

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.