pubtrait Euclid: Sized + Div<Self, Output = Self> + Rem<Self, Output = 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 /// ``` fn div_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); /// ``` fn rem_euclid(&self, v: &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 /// /// ``` /// # use num_traits::Euclid; /// 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)); /// ``` fn div_rem_euclid(&self, v: &Self) -> (Self, Self) {
(self.div_euclid(v), self.rem_euclid(v))
}
}
#[inline] fn rem_euclid(&self, v: &f64) -> f64 { let r = self % v; if r < 0.0 {
r + <f64 ascrate::float::FloatCore>::abs(*v)
} else {
r
}
}
}
pubtrait CheckedEuclid: Euclid { /// Performs euclid division that returns `None` instead of panicking on division by zero /// and instead of wrapping around on underflow and overflow. fn checked_div_euclid(&self, v: &Self) -> Option<Self>;
/// Finds the euclid remainder of dividing two numbers, checking for underflow, overflow and /// division by zero. If any of that happens, `None` is returned. fn checked_rem_euclid(&self, v: &Self) -> Option<Self>;
/// Returns both the quotient and remainder from checked Euclidean division. /// /// By default, it internally calls both `CheckedEuclid::checked_div_euclid` and `CheckedEuclid::checked_rem_euclid`, /// but it can be overridden in order to implement some optimization. /// # Examples /// /// ``` /// # use num_traits::CheckedEuclid; /// let x = 5u8; /// let y = 3u8; /// /// let div = CheckedEuclid::checked_div_euclid(&x, &y); /// let rem = CheckedEuclid::checked_rem_euclid(&x, &y); /// /// assert_eq!(Some((div.unwrap(), rem.unwrap())), CheckedEuclid::checked_div_rem_euclid(&x, &y)); /// ``` fn checked_div_rem_euclid(&self, v: &Self) -> Option<(Self, Self)> {
Some((self.checked_div_euclid(v)?, self.checked_rem_euclid(v)?))
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.