Skip to content

Commit 8be7e7b

Browse files
committed
Simplify some Ratio methods
1 parent 5b2cb8d commit 8be7e7b

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/rational.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,9 @@ impl<T> Neg for Ratio<T>
361361
type Output = Ratio<T>;
362362

363363
#[inline]
364-
fn neg(self) -> Ratio<T> { -&self }
364+
fn neg(self) -> Ratio<T> {
365+
Ratio::new_raw(-self.numer, self.denom)
366+
}
365367
}
366368

367369
impl<'a, T> Neg for &'a Ratio<T>
@@ -371,7 +373,7 @@ impl<'a, T> Neg for &'a Ratio<T>
371373

372374
#[inline]
373375
fn neg(self) -> Ratio<T> {
374-
Ratio::new_raw(-self.numer.clone(), self.denom.clone())
376+
-self.clone()
375377
}
376378
}
377379

@@ -385,7 +387,7 @@ impl<T: Clone + Integer>
385387

386388
#[inline]
387389
fn is_zero(&self) -> bool {
388-
*self == Zero::zero()
390+
self.numer.is_zero()
389391
}
390392
}
391393

@@ -436,20 +438,22 @@ impl<T: Clone + Integer + Signed> Signed for Ratio<T> {
436438

437439
#[inline]
438440
fn signum(&self) -> Ratio<T> {
439-
if *self > Zero::zero() {
440-
One::one()
441+
if self.is_positive() {
442+
Self::one()
441443
} else if self.is_zero() {
442-
Zero::zero()
444+
Self::zero()
443445
} else {
444-
- ::one::<Ratio<T>>()
446+
- Self::one()
445447
}
446448
}
447449

448450
#[inline]
449-
fn is_positive(&self) -> bool { *self > Zero::zero() }
451+
fn is_positive(&self) -> bool { !self.is_negative() }
450452

451453
#[inline]
452-
fn is_negative(&self) -> bool { *self < Zero::zero() }
454+
fn is_negative(&self) -> bool {
455+
self.numer.is_negative() ^ self.denom.is_negative()
456+
}
453457
}
454458

455459
/* String conversions */

0 commit comments

Comments
 (0)