Skip to content

Commit

Permalink
impl {Add,Sub,Mul,Div} for &BigRational
Browse files Browse the repository at this point in the history
Similar to the previous commit, this extends behavior of letting Ratios operate on primitives (and BigInt). In the case of `BigRational`, I imagine references to them will be more prevalent than the other types, hence this commit.
  • Loading branch information
turboladen committed Aug 22, 2018
1 parent 656fd4f commit fe7d5c0
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,15 @@ macro_rules! impl_bigint_ops_primitive {
}
}

impl<'a> Add<$primitive> for &'a BigRational {
type Output = BigRational;

#[inline]
fn add(self, rhs: $primitive) -> BigRational {
Ratio::new(self.numer.clone() + BigInt::from(rhs), self.denom.clone())
}
}

impl Sub<$primitive> for BigRational {
type Output = BigRational;

Expand All @@ -861,6 +870,15 @@ macro_rules! impl_bigint_ops_primitive {
}
}

impl<'a> Sub<$primitive> for &'a BigRational {
type Output = BigRational;

#[inline]
fn sub(self, rhs: $primitive) -> BigRational {
Ratio::new(self.numer.clone() - BigInt::from(rhs), self.denom.clone())
}
}

impl Mul<$primitive> for BigRational {
type Output = BigRational;

Expand All @@ -870,6 +888,15 @@ macro_rules! impl_bigint_ops_primitive {
}
}

impl<'a> Mul<$primitive> for &'a BigRational {
type Output = BigRational;

#[inline]
fn mul(self, rhs: $primitive) -> BigRational {
Ratio::new(self.numer.clone() * BigInt::from(rhs), self.denom.clone())
}
}

impl Div<$primitive> for BigRational {
type Output = BigRational;

Expand All @@ -878,6 +905,15 @@ macro_rules! impl_bigint_ops_primitive {
Ratio::new(self.numer, self.denom * BigInt::from(rhs))
}
}

impl<'a> Div<$primitive> for &'a BigRational {
type Output = BigRational;

#[inline]
fn div(self, rhs: $primitive) -> BigRational {
Ratio::new(self.numer.clone(), self.denom.clone() * BigInt::from(rhs))
}
}
};
}

Expand Down Expand Up @@ -1959,6 +1995,9 @@ mod test {
fn test_isize(a: BigRational, b: isize, c: BigRational) {
assert_eq!(a + b, c);
}
fn test_isize_ref(a: &BigRational, b: isize, c: BigRational) {
assert_eq!(a + b, c);
}
fn test_usize(a: BigRational, b: usize, c: BigRational) {
assert_eq!(a + b, c);
}
Expand Down Expand Up @@ -1997,8 +2036,12 @@ mod test {
fn test_bigint(a: BigRational, b: BigInt, c: BigRational) {
assert_eq!(a + b, c);
}
fn test_bigint_ref(a: &BigRational, b: BigInt, c: BigRational) {
assert_eq!(a + b, c);
}

test_isize(to_big(_1), -2, to_big(_NEG1));
test_isize_ref(&to_big(_1), -2, to_big(_NEG1));
test_usize(to_big(_1), 1, to_big(_2));
test_i8(to_big(_1), -2, to_big(_NEG1));
test_u8(to_big(_1), 1, to_big(_2));
Expand All @@ -2013,6 +2056,7 @@ mod test {
#[cfg(has_i128)]
test_u128(to_big(_1), 1, to_big(_2));
test_bigint(to_big(_1), BigInt::from(1), to_big(_2));
test_bigint_ref(&to_big(_1), BigInt::from(1), to_big(_2));
}

#[test]
Expand Down Expand Up @@ -2206,6 +2250,9 @@ mod test {
fn test_isize(a: BigRational, b: isize, c: BigRational) {
assert_eq!(a - b, c);
}
fn test_isize_ref(a: &BigRational, b: isize, c: BigRational) {
assert_eq!(a - b, c);
}
fn test_usize(a: BigRational, b: usize, c: BigRational) {
assert_eq!(a - b, c);
}
Expand Down Expand Up @@ -2244,8 +2291,12 @@ mod test {
fn test_bigint(a: BigRational, b: BigInt, c: BigRational) {
assert_eq!(a - b, c);
}
fn test_bigint_ref(a: &BigRational, b: BigInt, c: BigRational) {
assert_eq!(a - b, c);
}

test_isize(to_big(_2), 1, to_big(_1));
test_isize_ref(&to_big(_2), 1, to_big(_1));
test_usize(to_big(_2), 1, to_big(_1));
test_i8(to_big(_2), 1, to_big(_1));
test_u8(to_big(_2), 1, to_big(_1));
Expand All @@ -2260,6 +2311,7 @@ mod test {
#[cfg(has_i128)]
test_u128(to_big(_2), 1, to_big(_1));
test_bigint(to_big(_2), BigInt::from(1), to_big(_1));
test_bigint_ref(&to_big(_2), BigInt::from(1), to_big(_1));
}

#[test]
Expand Down Expand Up @@ -2453,6 +2505,9 @@ mod test {
fn test_isize(a: BigRational, b: isize, c: BigRational) {
assert_eq!(a * b, c);
}
fn test_isize_ref(a: &BigRational, b: isize, c: BigRational) {
assert_eq!(a * b, c);
}
fn test_usize(a: BigRational, b: usize, c: BigRational) {
assert_eq!(a * b, c);
}
Expand Down Expand Up @@ -2491,8 +2546,12 @@ mod test {
fn test_bigint(a: BigRational, b: BigInt, c: BigRational) {
assert_eq!(a * b, c);
}
fn test_bigint_ref(a: &BigRational, b: BigInt, c: BigRational) {
assert_eq!(a * b, c);
}

test_isize(to_big(_1_2), -2, to_big(_NEG1));
test_isize_ref(&to_big(_1_2), -2, to_big(_NEG1));
test_usize(to_big(_1_2), 2, to_big(_1));
test_i8(to_big(_1_2), -2, to_big(_NEG1));
test_u8(to_big(_1_2), 2, to_big(_1));
Expand All @@ -2507,6 +2566,7 @@ mod test {
#[cfg(has_i128)]
test_u128(to_big(_1_2), 2, to_big(_1));
test_bigint(to_big(_1_2), BigInt::from(2), to_big(_1));
test_bigint_ref(&to_big(_1_2), BigInt::from(2), to_big(_1));
}

#[test]
Expand Down Expand Up @@ -2700,6 +2760,9 @@ mod test {
fn test_isize(a: BigRational, b: isize, c: BigRational) {
assert_eq!(a / b, c);
}
fn test_isize_ref(a: &BigRational, b: isize, c: BigRational) {
assert_eq!(a / b, c);
}
fn test_usize(a: BigRational, b: usize, c: BigRational) {
assert_eq!(a / b, c);
}
Expand Down Expand Up @@ -2738,8 +2801,12 @@ mod test {
fn test_bigint(a: BigRational, b: BigInt, c: BigRational) {
assert_eq!(a / b, c);
}
fn test_bigint_ref(a: &BigRational, b: BigInt, c: BigRational) {
assert_eq!(a / b, c);
}

test_isize(to_big(_2), -2isize, to_big(_NEG1));
test_isize_ref(&to_big(_2), -2isize, to_big(_NEG1));
test_usize(to_big(_2), 2usize, to_big(_1));
test_i8(to_big(_2), -2i8, to_big(_NEG1));
test_u8(to_big(_2), 2u8, to_big(_1));
Expand All @@ -2754,6 +2821,7 @@ mod test {
#[cfg(has_i128)]
test_u128(to_big(_2), 2, to_big(_1));
test_bigint(to_big(_2), BigInt::from(2), to_big(_1));
test_bigint_ref(&to_big(_2), BigInt::from(2), to_big(_1));
}

#[test]
Expand Down

0 comments on commit fe7d5c0

Please sign in to comment.