diff --git a/src/integer/z/arithmetic/add.rs b/src/integer/z/arithmetic/add.rs index 2d14adef..ae07915b 100644 --- a/src/integer/z/arithmetic/add.rs +++ b/src/integer/z/arithmetic/add.rs @@ -140,6 +140,7 @@ impl Add<&Q> for &Z { arithmetic_trait_borrowed_to_owned!(Add, add, Z, Q, Q); arithmetic_trait_mixed_borrowed_owned!(Add, add, Z, Q, Q); +arithmetic_between_types!(Add, add, Z, Q, f32 f64); impl Add<&Zq> for &Z { type Output = Zq; @@ -381,6 +382,26 @@ mod test_add_between_z_and_q { use super::Z; use crate::rational::Q; + /// Ensuring addition between different types is available + #[test] + fn availability() { + let a: Z = Z::from(42); + let b: Q = Q::from((5, 7)); + + let _: Q = &a + &b; + let _: Q = &a + b.clone(); + let _: Q = a.clone() + &b; + let _: Q = a.clone() + b; + let _: Q = &a + 0.5_f32; + let _: Q = &a + 0.5_f64; + let _: Q = a.clone() + 0.5_f32; + let _: Q = a.clone() + 0.5_f64; + let _: Q = 0.5_f32 + &a; + let _: Q = 0.5_f64 + &a; + let _: Q = 0.5_f32 + a.clone(); + let _: Q = 0.5_f64 + a.clone(); + } + /// Testing addition for [`Z`] and [`Q`] #[test] fn add() { diff --git a/src/integer/z/arithmetic/div.rs b/src/integer/z/arithmetic/div.rs index 32088192..c414aeb9 100644 --- a/src/integer/z/arithmetic/div.rs +++ b/src/integer/z/arithmetic/div.rs @@ -166,7 +166,17 @@ impl Div for &Z { arithmetic_trait_borrowed_to_owned!(Div, div, Z, Z, Q); arithmetic_trait_mixed_borrowed_owned!(Div, div, Z, Z, Q); -arithmetic_between_types!(Div, div, Z, Q, i64 i32 i16 i8 u64 u32 u16 u8); +arithmetic_between_types!(Div, div, Z, Q, i64 i32 i16 i8 u64 u32 u16 u8 f32 f64); + +#[test] +fn same() { + let a = Z::from(4) / 2; + let b = 4 / Z::from(2); + + println!("{a}, {b}"); + + assert_eq!(a, b); +} impl Div<&Q> for &Z { type Output = Q; @@ -583,6 +593,35 @@ mod test_div_between_z_and_q { use super::Z; use crate::rational::Q; + /// Ensuring division between different types is available + #[test] + fn availability() { + let a: Z = Z::from(42); + let b: Q = Q::from((5, 7)); + + let _: Q = &a / &b; + let _: Q = &a / b.clone(); + let _: Q = a.clone() / &b; + let _: Q = a.clone() / b; + let _: Q = &a / 0.5_f32; + let _: Q = &a / 0.5_f64; + let _: Q = a.clone() / 0.5_f32; + let _: Q = a.clone() / 0.5_f64; + let _: Q = 0.5_f32 / &a; + let _: Q = 0.5_f64 / &a; + let _: Q = 0.5_f32 / a.clone(); + let _: Q = 0.5_f64 / a.clone(); + } + + /// Ensures that division is performed the right way around + #[test] + fn order_retained() { + let a = Z::from(4); + + assert_eq!(2, &a / 2); + assert_eq!(Q::from((1, 2)), 2 / &a); + } + /// Testing division for [`Z`] and [`Q`] #[test] fn div() { diff --git a/src/integer/z/arithmetic/mul.rs b/src/integer/z/arithmetic/mul.rs index 9caae5b8..7550236d 100644 --- a/src/integer/z/arithmetic/mul.rs +++ b/src/integer/z/arithmetic/mul.rs @@ -139,6 +139,7 @@ impl Mul<&Q> for &Z { arithmetic_trait_borrowed_to_owned!(Mul, mul, Z, Q, Q); arithmetic_trait_mixed_borrowed_owned!(Mul, mul, Z, Q, Q); +arithmetic_between_types!(Mul, mul, Z, Q, f32 f64); impl Mul<&Zq> for &Z { type Output = Zq; @@ -447,6 +448,26 @@ mod test_mul_between_z_and_q { use super::Z; use crate::rational::Q; + /// Ensuring multiplication between different types is available + #[test] + fn availability() { + let a: Z = Z::from(42); + let b: Q = Q::from((5, 7)); + + let _: Q = &a * &b; + let _: Q = &a * b.clone(); + let _: Q = a.clone() * &b; + let _: Q = a.clone() * b; + let _: Q = &a * 0.5_f32; + let _: Q = &a * 0.5_f64; + let _: Q = a.clone() * 0.5_f32; + let _: Q = a.clone() * 0.5_f64; + let _: Q = 0.5_f32 * &a; + let _: Q = 0.5_f64 * &a; + let _: Q = 0.5_f32 * a.clone(); + let _: Q = 0.5_f64 * a.clone(); + } + /// Testing multiplication for [`Z`] and [`Q`] #[test] fn mul() { diff --git a/src/integer/z/arithmetic/sub.rs b/src/integer/z/arithmetic/sub.rs index 69813cb7..050dce00 100644 --- a/src/integer/z/arithmetic/sub.rs +++ b/src/integer/z/arithmetic/sub.rs @@ -141,6 +141,7 @@ impl Sub<&Q> for &Z { arithmetic_trait_borrowed_to_owned!(Sub, sub, Z, Q, Q); arithmetic_trait_mixed_borrowed_owned!(Sub, sub, Z, Q, Q); +arithmetic_between_types!(Sub, sub, Z, Q, f64 f32); impl Sub<&Zq> for &Z { type Output = Zq; @@ -397,6 +398,35 @@ mod test_sub_between_z_and_q { use super::Z; use crate::rational::Q; + /// Ensuring subtraction between different types is available + #[test] + fn availability() { + let a: Z = Z::from(42); + let b: Q = Q::from((5, 7)); + + let _: Q = &a - &b; + let _: Q = &a - b.clone(); + let _: Q = a.clone() - &b; + let _: Q = a.clone() - b; + let _: Q = &a - 0.5_f32; + let _: Q = &a - 0.5_f64; + let _: Q = a.clone() - 0.5_f32; + let _: Q = a.clone() - 0.5_f64; + let _: Q = 0.5_f32 - &a; + let _: Q = 0.5_f64 - &a; + let _: Q = 0.5_f32 - a.clone(); + let _: Q = 0.5_f64 - a.clone(); + } + + /// Ensures that division is performed the right way around + #[test] + fn order_retained() { + let a = Z::from(4); + + assert_eq!(2, &a - 2); + assert_eq!(Q::from((-2, 1)), 2 - &a); + } + /// Testing subtraction for [`Z`] and [`Q`] #[test] fn sub() { diff --git a/src/macros/arithmetics.rs b/src/macros/arithmetics.rs index 7473345b..081755f7 100644 --- a/src/macros/arithmetics.rs +++ b/src/macros/arithmetics.rs @@ -131,7 +131,7 @@ macro_rules! arithmetic_between_types { paste::paste! { #[doc = "Documentation at [`" $type "::" $trait_function "`]."] fn $trait_function(self, other: &$other_type) -> Self::Output { - self.$trait_function($type::from(*other)) + self.$trait_function($output_type::from(*other)) } } } @@ -145,7 +145,7 @@ macro_rules! arithmetic_between_types { paste::paste! { #[doc = "Documentation at [`" $type "::" $trait_function "`]."] fn $trait_function(self, other: &$type) -> Self::Output { - $type::from(*self).$trait_function(other) + $output_type::from(*self).$trait_function(other) } } }