Skip to content

Commit 225ac21

Browse files
committed
Update impl of Round for Ratio
1 parent 48c2418 commit 225ac21

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/libstd/num/rational.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,6 @@ impl<T: Copy + Num + Ord>
204204
/* Utils */
205205
impl<T: Copy + Num + Ord>
206206
Round for Ratio<T> {
207-
fn round(&self, mode: num::RoundMode) -> Ratio<T> {
208-
match mode {
209-
num::RoundUp => { self.ceil() }
210-
num::RoundDown => { self.floor()}
211-
num::RoundToZero => { Ratio::from_integer(self.numer / self.denom) }
212-
num::RoundFromZero => {
213-
if *self < Zero::zero() {
214-
Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
215-
} else {
216-
Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
217-
}
218-
}
219-
}
220-
}
221207
222208
fn floor(&self) -> Ratio<T> {
223209
if *self < Zero::zero() {
@@ -226,13 +212,29 @@ impl<T: Copy + Num + Ord>
226212
Ratio::from_integer(self.numer / self.denom)
227213
}
228214
}
215+
229216
fn ceil(&self) -> Ratio<T> {
230217
if *self < Zero::zero() {
231218
Ratio::from_integer(self.numer / self.denom)
232219
} else {
233220
Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
234221
}
235222
}
223+
224+
#[inline(always)]
225+
fn round(&self) -> Ratio<T> {
226+
if *self < Zero::zero() {
227+
Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
228+
} else {
229+
Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
230+
}
231+
}
232+
233+
#[inline(always)]
234+
fn trunc(&self) -> Ratio<T> {
235+
Ratio::from_integer(self.numer / self.denom)
236+
}
237+
236238
fn fract(&self) -> Ratio<T> {
237239
Ratio::new_raw(self.numer % self.denom, self.denom)
238240
}
@@ -421,18 +423,18 @@ mod test {
421423
fn test_round() {
422424
assert_eq!(_1_2.ceil(), _1);
423425
assert_eq!(_1_2.floor(), _0);
424-
assert_eq!(_1_2.round(num::RoundToZero), _0);
425-
assert_eq!(_1_2.round(num::RoundFromZero), _1);
426+
assert_eq!(_1_2.round(), _1);
427+
assert_eq!(_1_2.trunc(), _0);
426428
427429
assert_eq!(_neg1_2.ceil(), _0);
428430
assert_eq!(_neg1_2.floor(), -_1);
429-
assert_eq!(_neg1_2.round(num::RoundToZero), _0);
430-
assert_eq!(_neg1_2.round(num::RoundFromZero), -_1);
431+
assert_eq!(_neg1_2.round(), -_1);
432+
assert_eq!(_neg1_2.trunc(), _0);
431433
432434
assert_eq!(_1.ceil(), _1);
433435
assert_eq!(_1.floor(), _1);
434-
assert_eq!(_1.round(num::RoundToZero), _1);
435-
assert_eq!(_1.round(num::RoundFromZero), _1);
436+
assert_eq!(_1.round(), _1);
437+
assert_eq!(_1.trunc(), _1);
436438
}
437439
438440
#[test]

0 commit comments

Comments
 (0)