diff options
| author | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2014-04-18 12:48:48 +1000 |
|---|---|---|
| committer | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2014-04-19 10:41:45 +1000 |
| commit | fe472020347d8eeb727f3a31e9cdc7268bb579f6 (patch) | |
| tree | 63cabb323fb76aea7150d09744e0d8c8275b40d6 /src/libnum | |
| parent | b75683cadf6c4c55360202cd6a0106be80532451 (diff) | |
| download | rust-fe472020347d8eeb727f3a31e9cdc7268bb579f6.tar.gz rust-fe472020347d8eeb727f3a31e9cdc7268bb579f6.zip | |
Merge the Round trait into the Float trait
Move the rounding functions into the `std::num::Float` trait and then remove `std::num::Round`. This continues the flattening of the numeric traits tracked in #10387. The aim is to make `std::num` very simple and tied to the built in types, leaving the definition of more complex numeric towers to third-party libraries. [breaking-change]
Diffstat (limited to 'src/libnum')
| -rw-r--r-- | src/libnum/rational.rs | 75 |
1 files changed, 35 insertions, 40 deletions
diff --git a/src/libnum/rational.rs b/src/libnum/rational.rs index e6b63f23741..8f2efc8626b 100644 --- a/src/libnum/rational.rs +++ b/src/libnum/rational.rs @@ -15,7 +15,7 @@ use Integer; use std::cmp; use std::fmt; use std::from_str::FromStr; -use std::num::{Zero,One,ToStrRadix,FromStrRadix,Round}; +use std::num::{Zero, One, ToStrRadix, FromStrRadix}; use bigint::{BigInt, BigUint, Sign, Plus, Minus}; /// Represents the ratio between 2 numbers. @@ -113,6 +113,40 @@ impl<T: Clone + Integer + Ord> pub fn recip(&self) -> Ratio<T> { Ratio::new_raw(self.denom.clone(), self.numer.clone()) } + + pub fn floor(&self) -> Ratio<T> { + if *self < Zero::zero() { + Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom) + } else { + Ratio::from_integer(self.numer / self.denom) + } + } + + pub fn ceil(&self) -> Ratio<T> { + if *self < Zero::zero() { + Ratio::from_integer(self.numer / self.denom) + } else { + Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom) + } + } + + #[inline] + pub fn round(&self) -> Ratio<T> { + if *self < Zero::zero() { + Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom) + } else { + Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom) + } + } + + #[inline] + pub fn trunc(&self) -> Ratio<T> { + Ratio::from_integer(self.numer / self.denom) + } + + pub fn fract(&self) -> Ratio<T> { + Ratio::new_raw(self.numer % self.denom, self.denom.clone()) + } } impl Ratio<BigInt> { @@ -238,45 +272,6 @@ impl<T: Clone + Integer + Ord> impl<T: Clone + Integer + Ord> Num for Ratio<T> {} -/* Utils */ -impl<T: Clone + Integer + Ord> - Round for Ratio<T> { - - fn floor(&self) -> Ratio<T> { - if *self < Zero::zero() { - Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom) - } else { - Ratio::from_integer(self.numer / self.denom) - } - } - - fn ceil(&self) -> Ratio<T> { - if *self < Zero::zero() { - Ratio::from_integer(self.numer / self.denom) - } else { - Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom) - } - } - - #[inline] - fn round(&self) -> Ratio<T> { - if *self < Zero::zero() { - Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom) - } else { - Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom) - } - } - - #[inline] - fn trunc(&self) -> Ratio<T> { - Ratio::from_integer(self.numer / self.denom) - } - - fn fract(&self) -> Ratio<T> { - Ratio::new_raw(self.numer % self.denom, self.denom.clone()) - } -} - /* String conversions */ impl<T: fmt::Show> fmt::Show for Ratio<T> { /// Renders as `numer/denom`. |
