@@ -204,20 +204,6 @@ impl<T: Copy + Num + Ord>
204
204
/* Utils */
205
205
impl<T: Copy + Num + Ord>
206
206
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
- }
221
207
222
208
fn floor(&self) -> Ratio<T> {
223
209
if *self < Zero::zero() {
@@ -226,13 +212,29 @@ impl<T: Copy + Num + Ord>
226
212
Ratio::from_integer(self.numer / self.denom)
227
213
}
228
214
}
215
+
229
216
fn ceil(&self) -> Ratio<T> {
230
217
if *self < Zero::zero() {
231
218
Ratio::from_integer(self.numer / self.denom)
232
219
} else {
233
220
Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
234
221
}
235
222
}
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
+
236
238
fn fract(&self) -> Ratio<T> {
237
239
Ratio::new_raw(self.numer % self.denom, self.denom)
238
240
}
@@ -421,18 +423,18 @@ mod test {
421
423
fn test_round() {
422
424
assert_eq!(_1_2.ceil(), _1);
423
425
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 );
426
428
427
429
assert_eq!(_neg1_2.ceil(), _0);
428
430
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 );
431
433
432
434
assert_eq!(_1.ceil(), _1);
433
435
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);
436
438
}
437
439
438
440
#[test]
0 commit comments