num::traits

Trait Euclid

Source
pub trait Euclid:
    Sized
    + Div<Output = Self>
    + Rem<Output = Self> {
    // Required methods
    fn div_euclid(&self, v: &Self) -> Self;
    fn rem_euclid(&self, v: &Self) -> Self;

    // Provided method
    fn div_rem_euclid(&self, v: &Self) -> (Self, Self) { ... }
}

Required Methods§

Source

fn div_euclid(&self, v: &Self) -> Self

Calculates Euclidean division, the matching method for rem_euclid.

This computes the integer n such that self = n * v + self.rem_euclid(v). In other words, the result is self / v rounded to the integer n such that self >= n * v.

§Examples
use num_traits::Euclid;

let a: i32 = 7;
let b: i32 = 4;
assert_eq!(Euclid::div_euclid(&a, &b), 1); // 7 > 4 * 1
assert_eq!(Euclid::div_euclid(&-a, &b), -2); // -7 >= 4 * -2
assert_eq!(Euclid::div_euclid(&a, &-b), -1); // 7 >= -4 * -1
assert_eq!(Euclid::div_euclid(&-a, &-b), 2); // -7 >= -4 * 2
Source

fn rem_euclid(&self, v: &Self) -> Self

Calculates the least nonnegative remainder of self (mod v).

In particular, the return value r satisfies 0.0 <= r < v.abs() in most cases. However, due to a floating point round-off error it can result in r == v.abs(), violating the mathematical definition, if self is much smaller than v.abs() in magnitude and self < 0.0. This result is not an element of the function’s codomain, but it is the closest floating point number in the real numbers and thus fulfills the property self == self.div_euclid(v) * v + self.rem_euclid(v) approximatively.

§Examples
use num_traits::Euclid;

let a: i32 = 7;
let b: i32 = 4;
assert_eq!(Euclid::rem_euclid(&a, &b), 3);
assert_eq!(Euclid::rem_euclid(&-a, &b), 1);
assert_eq!(Euclid::rem_euclid(&a, &-b), 3);
assert_eq!(Euclid::rem_euclid(&-a, &-b), 1);

Provided Methods§

Source

fn div_rem_euclid(&self, v: &Self) -> (Self, Self)

Returns both the quotient and remainder from Euclidean division.

By default, it internally calls both Euclid::div_euclid and Euclid::rem_euclid, but it can be overridden in order to implement some optimization.

§Examples
let x = 5u8;
let y = 3u8;

let div = Euclid::div_euclid(&x, &y);
let rem = Euclid::rem_euclid(&x, &y);

assert_eq!((div, rem), Euclid::div_rem_euclid(&x, &y));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Euclid for f32

Source§

fn div_euclid(&self, v: &f32) -> f32

Source§

fn rem_euclid(&self, v: &f32) -> f32

Source§

impl Euclid for f64

Source§

fn div_euclid(&self, v: &f64) -> f64

Source§

fn rem_euclid(&self, v: &f64) -> f64

Source§

impl Euclid for i8

Source§

fn div_euclid(&self, v: &i8) -> i8

Source§

fn rem_euclid(&self, v: &i8) -> i8

Source§

impl Euclid for i16

Source§

fn div_euclid(&self, v: &i16) -> i16

Source§

fn rem_euclid(&self, v: &i16) -> i16

Source§

impl Euclid for i32

Source§

fn div_euclid(&self, v: &i32) -> i32

Source§

fn rem_euclid(&self, v: &i32) -> i32

Source§

impl Euclid for i64

Source§

fn div_euclid(&self, v: &i64) -> i64

Source§

fn rem_euclid(&self, v: &i64) -> i64

Source§

impl Euclid for i128

Source§

fn div_euclid(&self, v: &i128) -> i128

Source§

fn rem_euclid(&self, v: &i128) -> i128

Source§

impl Euclid for isize

Source§

fn div_euclid(&self, v: &isize) -> isize

Source§

fn rem_euclid(&self, v: &isize) -> isize

Source§

impl Euclid for u8

Source§

fn div_euclid(&self, v: &u8) -> u8

Source§

fn rem_euclid(&self, v: &u8) -> u8

Source§

impl Euclid for u16

Source§

fn div_euclid(&self, v: &u16) -> u16

Source§

fn rem_euclid(&self, v: &u16) -> u16

Source§

impl Euclid for u32

Source§

fn div_euclid(&self, v: &u32) -> u32

Source§

fn rem_euclid(&self, v: &u32) -> u32

Source§

impl Euclid for u64

Source§

fn div_euclid(&self, v: &u64) -> u64

Source§

fn rem_euclid(&self, v: &u64) -> u64

Source§

impl Euclid for u128

Source§

fn div_euclid(&self, v: &u128) -> u128

Source§

fn rem_euclid(&self, v: &u128) -> u128

Source§

impl Euclid for usize

Source§

fn div_euclid(&self, v: &usize) -> usize

Source§

fn rem_euclid(&self, v: &usize) -> usize

Implementors§