diff --git a/Cargo.toml b/Cargo.toml index 8bc4c4d..303c0d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ version = "0.1.36" default-features = false [dependencies.num-traits] -version = "0.2.0" +version = "0.2.1" default-features = false features = ["std"] diff --git a/src/lib.rs b/src/lib.rs index 7929297..fedf210 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,7 +37,7 @@ use std::str::FromStr; use bigint::{BigInt, BigUint, Sign}; use integer::Integer; -use traits::{FromPrimitive, Float, PrimInt, Num, Signed, Zero, One, Bounded, NumCast, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv}; +use traits::{FromPrimitive, Float, PrimInt, Num, Signed, Zero, One, Bounded, Inv, NumCast, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv}; /// Represents the ratio between 2 numbers. #[derive(Copy, Clone, Debug)] @@ -105,7 +105,7 @@ impl Ratio { /// Returns true if the rational number is an integer (denominator is 1). #[inline] pub fn is_integer(&self) -> bool { - self.denom == One::one() + self.denom.is_one() } /// Puts self into lowest terms, with denom > 0. @@ -715,6 +715,28 @@ impl<'a, T> Neg for &'a Ratio } } +impl Inv for Ratio + where T: Clone + Integer +{ + type Output = Ratio; + + #[inline] + fn inv(self) -> Ratio { + self.recip() + } +} + +impl<'a, T> Inv for &'a Ratio + where T: Clone + Integer +{ + type Output = Ratio; + + #[inline] + fn inv(self) -> Ratio { + self.recip() + } +} + // Constants impl Zero for Ratio { #[inline] @@ -733,6 +755,11 @@ impl One for Ratio { fn one() -> Ratio { Ratio::new_raw(One::one(), One::one()) } + + #[inline] + fn is_one(&self) -> bool { + self.numer == self.denom + } } impl Num for Ratio { @@ -812,7 +839,7 @@ impl fmt::Display for Ratio { /// Renders as `numer/denom`. If denom=1, renders as numer. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - if self.denom == One::one() { + if self.denom.is_one() { write!(f, "{}", self.numer) } else { write!(f, "{}/{}", self.numer, self.denom)