Skip to content

Commit

Permalink
Update impl of Round for Ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab committed Apr 25, 2013
1 parent 48c2418 commit 225ac21
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions src/libstd/num/rational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,20 +204,6 @@ impl<T: Copy + Num + Ord>
/* Utils */
impl<T: Copy + Num + Ord>
Round for Ratio<T> {
fn round(&self, mode: num::RoundMode) -> Ratio<T> {
match mode {
num::RoundUp => { self.ceil() }
num::RoundDown => { self.floor()}
num::RoundToZero => { Ratio::from_integer(self.numer / self.denom) }
num::RoundFromZero => {
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)
}
}
}
}

fn floor(&self) -> Ratio<T> {
if *self < Zero::zero() {
Expand All @@ -226,13 +212,29 @@ impl<T: Copy + Num + Ord>
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(always)]
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(always)]
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)
}
Expand Down Expand Up @@ -421,18 +423,18 @@ mod test {
fn test_round() {
assert_eq!(_1_2.ceil(), _1);
assert_eq!(_1_2.floor(), _0);
assert_eq!(_1_2.round(num::RoundToZero), _0);
assert_eq!(_1_2.round(num::RoundFromZero), _1);
assert_eq!(_1_2.round(), _1);
assert_eq!(_1_2.trunc(), _0);

assert_eq!(_neg1_2.ceil(), _0);
assert_eq!(_neg1_2.floor(), -_1);
assert_eq!(_neg1_2.round(num::RoundToZero), _0);
assert_eq!(_neg1_2.round(num::RoundFromZero), -_1);
assert_eq!(_neg1_2.round(), -_1);
assert_eq!(_neg1_2.trunc(), _0);

assert_eq!(_1.ceil(), _1);
assert_eq!(_1.floor(), _1);
assert_eq!(_1.round(num::RoundToZero), _1);
assert_eq!(_1.round(num::RoundFromZero), _1);
assert_eq!(_1.round(), _1);
assert_eq!(_1.trunc(), _1);
}

#[test]
Expand Down

8 comments on commit 225ac21

@bors
Copy link
Contributor

@bors bors commented on 225ac21 Apr 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at brendanzab@225ac21

@bors
Copy link
Contributor

@bors bors commented on 225ac21 Apr 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging bjz/rust/numeric-traits = 225ac21 into auto

@bors
Copy link
Contributor

@bors bors commented on 225ac21 Apr 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bjz/rust/numeric-traits = 225ac21 merged ok, testing candidate = 195d868e

@bors
Copy link
Contributor

@bors bors commented on 225ac21 Apr 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at brendanzab@225ac21

@bors
Copy link
Contributor

@bors bors commented on 225ac21 Apr 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging bjz/rust/numeric-traits = 225ac21 into auto

@bors
Copy link
Contributor

@bors bors commented on 225ac21 Apr 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bjz/rust/numeric-traits = 225ac21 merged ok, testing candidate = ac69ee4

@bors
Copy link
Contributor

@bors bors commented on 225ac21 Apr 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 225ac21 Apr 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding incoming to auto = ac69ee4

Please sign in to comment.