diff options
Diffstat (limited to 'src/libnum')
| -rw-r--r-- | src/libnum/bigint.rs | 18 | ||||
| -rw-r--r-- | src/libnum/integer.rs | 45 | ||||
| -rw-r--r-- | src/libnum/rational.rs | 27 |
3 files changed, 62 insertions, 28 deletions
diff --git a/src/libnum/bigint.rs b/src/libnum/bigint.rs index acba750aaf4..4dd3817e475 100644 --- a/src/libnum/bigint.rs +++ b/src/libnum/bigint.rs @@ -514,9 +514,14 @@ impl Integer for BigUint { #[inline] fn lcm(&self, other: &BigUint) -> BigUint { ((*self * *other) / self.gcd(other)) } - /// Returns `true` if the number can be divided by `other` without leaving a remainder. + /// Deprecated, use `is_multiple_of` instead. + #[deprecated = "function renamed to `is_multiple_of`"] #[inline] - fn divides(&self, other: &BigUint) -> bool { (*self % *other).is_zero() } + fn divides(&self, other: &BigUint) -> bool { return self.is_multiple_of(other); } + + /// Returns `true` if the number is a multiple of `other`. + #[inline] + fn is_multiple_of(&self, other: &BigUint) -> bool { (*self % *other).is_zero() } /// Returns `true` if the number is divisible by `2`. #[inline] @@ -1112,9 +1117,14 @@ impl Integer for BigInt { BigInt::from_biguint(Plus, self.data.lcm(&other.data)) } - /// Returns `true` if the number can be divided by `other` without leaving a remainder. + /// Deprecated, use `is_multiple_of` instead. + #[deprecated = "function renamed to `is_multiple_of`"] + #[inline] + fn divides(&self, other: &BigInt) -> bool { return self.is_multiple_of(other); } + + /// Returns `true` if the number is a multiple of `other`. #[inline] - fn divides(&self, other: &BigInt) -> bool { self.data.divides(&other.data) } + fn is_multiple_of(&self, other: &BigInt) -> bool { self.data.is_multiple_of(&other.data) } /// Returns `true` if the number is divisible by `2`. #[inline] diff --git a/src/libnum/integer.rs b/src/libnum/integer.rs index bcaebbd1368..b06e2b448d4 100644 --- a/src/libnum/integer.rs +++ b/src/libnum/integer.rs @@ -77,16 +77,20 @@ pub trait Integer: Num + PartialOrd /// ~~~ fn lcm(&self, other: &Self) -> Self; - /// Returns `true` if `other` divides evenly into `self`. + /// Deprecated, use `is_multiple_of` instead. + #[deprecated = "function renamed to `is_multiple_of`"] + fn divides(&self, other: &Self) -> bool; + + /// Returns `true` if `other` is a multiple of `self`. /// /// # Examples /// /// ~~~ /// # use num::Integer; - /// assert_eq!(9i.divides(&3), true); - /// assert_eq!(3i.divides(&9), false); + /// assert_eq!(9i.is_multiple_of(&3), true); + /// assert_eq!(3i.is_multiple_of(&9), false); /// ~~~ - fn divides(&self, other: &Self) -> bool; + fn is_multiple_of(&self, other: &Self) -> bool; /// Returns `true` if the number is even. /// @@ -231,10 +235,14 @@ macro_rules! impl_integer_for_int { ((*self * *other) / self.gcd(other)).abs() } - /// Returns `true` if the number can be divided by `other` without - /// leaving a remainder + /// Deprecated, use `is_multiple_of` instead. + #[deprecated = "function renamed to `is_multiple_of`"] + #[inline] + fn divides(&self, other: &$T) -> bool { return self.is_multiple_of(other); } + + /// Returns `true` if the number is a multiple of `other`. #[inline] - fn divides(&self, other: &$T) -> bool { *self % *other == 0 } + fn is_multiple_of(&self, other: &$T) -> bool { *self % *other == 0 } /// Returns `true` if the number is divisible by `2` #[inline] @@ -393,21 +401,26 @@ macro_rules! impl_integer_for_uint { n } - /// Calculates the Lowest Common Multiple (LCM) of the number and `other` + /// Calculates the Lowest Common Multiple (LCM) of the number and `other`. #[inline] fn lcm(&self, other: &$T) -> $T { (*self * *other) / self.gcd(other) } - /// Returns `true` if the number can be divided by `other` without leaving a remainder + /// Deprecated, use `is_multiple_of` instead. + #[deprecated = "function renamed to `is_multiple_of`"] #[inline] - fn divides(&self, other: &$T) -> bool { *self % *other == 0 } + fn divides(&self, other: &$T) -> bool { return self.is_multiple_of(other); } - /// Returns `true` if the number is divisible by `2` + /// Returns `true` if the number is a multiple of `other`. + #[inline] + fn is_multiple_of(&self, other: &$T) -> bool { *self % *other == 0 } + + /// Returns `true` if the number is divisible by `2`. #[inline] fn is_even(&self) -> bool { self & 1 == 0 } - /// Returns `true` if the number is not divisible by `2` + /// Returns `true` if the number is not divisible by `2`. #[inline] fn is_odd(&self) -> bool { !self.is_even() } } @@ -449,10 +462,10 @@ macro_rules! impl_integer_for_uint { } #[test] - fn test_divides() { - assert!((6 as $T).divides(&(6 as $T))); - assert!((6 as $T).divides(&(3 as $T))); - assert!((6 as $T).divides(&(1 as $T))); + fn test_is_multiple_of() { + assert!((6 as $T).is_multiple_of(&(6 as $T))); + assert!((6 as $T).is_multiple_of(&(3 as $T))); + assert!((6 as $T).is_multiple_of(&(1 as $T))); } #[test] diff --git a/src/libnum/rational.rs b/src/libnum/rational.rs index a279ede6fa5..e0f6b4fb9af 100644 --- a/src/libnum/rational.rs +++ b/src/libnum/rational.rs @@ -38,13 +38,13 @@ pub type BigRational = Ratio<BigInt>; impl<T: Clone + Integer + PartialOrd> Ratio<T> { - /// Create a ratio representing the integer `t`. + /// Creates a ratio representing the integer `t`. #[inline] pub fn from_integer(t: T) -> Ratio<T> { Ratio::new_raw(t, One::one()) } - /// Create a ratio without checking for `denom == 0` or reducing. + /// Creates a ratio without checking for `denom == 0` or reducing. #[inline] pub fn new_raw(numer: T, denom: T) -> Ratio<T> { Ratio { numer: numer, denom: denom } @@ -61,7 +61,7 @@ impl<T: Clone + Integer + PartialOrd> ret } - /// Convert to an integer. + /// Converts to an integer. #[inline] pub fn to_integer(&self) -> T { self.trunc().numer @@ -79,7 +79,7 @@ impl<T: Clone + Integer + PartialOrd> &self.denom } - /// Return true if the rational number is an integer (denominator is 1). + /// Returns true if the rational number is an integer (denominator is 1). #[inline] pub fn is_integer(&self) -> bool { self.denom == One::one() @@ -103,19 +103,21 @@ impl<T: Clone + Integer + PartialOrd> } } - /// Return a `reduce`d copy of self. + /// Returns a `reduce`d copy of self. pub fn reduced(&self) -> Ratio<T> { let mut ret = self.clone(); ret.reduce(); ret } - /// Return the reciprocal + /// Returns the reciprocal. #[inline] pub fn recip(&self) -> Ratio<T> { Ratio::new_raw(self.denom.clone(), self.numer.clone()) } + /// Rounds towards minus infinity. + #[inline] pub fn floor(&self) -> Ratio<T> { if *self < Zero::zero() { Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom) @@ -124,6 +126,8 @@ impl<T: Clone + Integer + PartialOrd> } } + /// Rounds towards plus infinity. + #[inline] pub fn ceil(&self) -> Ratio<T> { if *self < Zero::zero() { Ratio::from_integer(self.numer / self.denom) @@ -132,8 +136,12 @@ impl<T: Clone + Integer + PartialOrd> } } + /// Rounds to the nearest integer. Rounds half-way cases away from zero. + /// + /// Note: This function is currently broken and always rounds away from zero. #[inline] pub fn round(&self) -> Ratio<T> { + // FIXME(#15826) if *self < Zero::zero() { Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom) } else { @@ -141,18 +149,21 @@ impl<T: Clone + Integer + PartialOrd> } } + /// Rounds towards zero. #[inline] pub fn trunc(&self) -> Ratio<T> { Ratio::from_integer(self.numer / self.denom) } + ///Returns the fractional part of a number. + #[inline] pub fn fract(&self) -> Ratio<T> { Ratio::new_raw(self.numer % self.denom, self.denom.clone()) } } impl Ratio<BigInt> { - /// Converts a float into a rational number + /// Converts a float into a rational number. pub fn from_float<T: Float>(f: T) -> Option<BigRational> { if !f.is_finite() { return None; @@ -328,7 +339,7 @@ impl<T: ToStrRadix> ToStrRadix for Ratio<T> { impl<T: FromStr + Clone + Integer + PartialOrd> FromStr for Ratio<T> { - /// Parses `numer/denom` or just `numer` + /// Parses `numer/denom` or just `numer`. fn from_str(s: &str) -> Option<Ratio<T>> { let mut split = s.splitn('/', 1); |
