Skip to content

Commit 3026728

Browse files
authored
Merge pull request #523 from qfall/missing_cross_types
Arithmetic between `Z` and `f64/f32`
2 parents 26f686b + 6ee45d0 commit 3026728

5 files changed

Lines changed: 114 additions & 3 deletions

File tree

src/integer/z/arithmetic/add.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ impl Add<&Q> for &Z {
140140

141141
arithmetic_trait_borrowed_to_owned!(Add, add, Z, Q, Q);
142142
arithmetic_trait_mixed_borrowed_owned!(Add, add, Z, Q, Q);
143+
arithmetic_between_types!(Add, add, Z, Q, f32 f64);
143144

144145
impl Add<&Zq> for &Z {
145146
type Output = Zq;
@@ -381,6 +382,26 @@ mod test_add_between_z_and_q {
381382
use super::Z;
382383
use crate::rational::Q;
383384

385+
/// Ensuring addition between different types is available
386+
#[test]
387+
fn availability() {
388+
let a: Z = Z::from(42);
389+
let b: Q = Q::from((5, 7));
390+
391+
let _: Q = &a + &b;
392+
let _: Q = &a + b.clone();
393+
let _: Q = a.clone() + &b;
394+
let _: Q = a.clone() + b;
395+
let _: Q = &a + 0.5_f32;
396+
let _: Q = &a + 0.5_f64;
397+
let _: Q = a.clone() + 0.5_f32;
398+
let _: Q = a.clone() + 0.5_f64;
399+
let _: Q = 0.5_f32 + &a;
400+
let _: Q = 0.5_f64 + &a;
401+
let _: Q = 0.5_f32 + a.clone();
402+
let _: Q = 0.5_f64 + a.clone();
403+
}
404+
384405
/// Testing addition for [`Z`] and [`Q`]
385406
#[test]
386407
fn add() {

src/integer/z/arithmetic/div.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,17 @@ impl Div for &Z {
166166

167167
arithmetic_trait_borrowed_to_owned!(Div, div, Z, Z, Q);
168168
arithmetic_trait_mixed_borrowed_owned!(Div, div, Z, Z, Q);
169-
arithmetic_between_types!(Div, div, Z, Q, i64 i32 i16 i8 u64 u32 u16 u8);
169+
arithmetic_between_types!(Div, div, Z, Q, i64 i32 i16 i8 u64 u32 u16 u8 f32 f64);
170+
171+
#[test]
172+
fn same() {
173+
let a = Z::from(4) / 2;
174+
let b = 4 / Z::from(2);
175+
176+
println!("{a}, {b}");
177+
178+
assert_eq!(a, b);
179+
}
170180

171181
impl Div<&Q> for &Z {
172182
type Output = Q;
@@ -583,6 +593,35 @@ mod test_div_between_z_and_q {
583593
use super::Z;
584594
use crate::rational::Q;
585595

596+
/// Ensuring division between different types is available
597+
#[test]
598+
fn availability() {
599+
let a: Z = Z::from(42);
600+
let b: Q = Q::from((5, 7));
601+
602+
let _: Q = &a / &b;
603+
let _: Q = &a / b.clone();
604+
let _: Q = a.clone() / &b;
605+
let _: Q = a.clone() / b;
606+
let _: Q = &a / 0.5_f32;
607+
let _: Q = &a / 0.5_f64;
608+
let _: Q = a.clone() / 0.5_f32;
609+
let _: Q = a.clone() / 0.5_f64;
610+
let _: Q = 0.5_f32 / &a;
611+
let _: Q = 0.5_f64 / &a;
612+
let _: Q = 0.5_f32 / a.clone();
613+
let _: Q = 0.5_f64 / a.clone();
614+
}
615+
616+
/// Ensures that division is performed the right way around
617+
#[test]
618+
fn order_retained() {
619+
let a = Z::from(4);
620+
621+
assert_eq!(2, &a / 2);
622+
assert_eq!(Q::from((1, 2)), 2 / &a);
623+
}
624+
586625
/// Testing division for [`Z`] and [`Q`]
587626
#[test]
588627
fn div() {

src/integer/z/arithmetic/mul.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ impl Mul<&Q> for &Z {
139139

140140
arithmetic_trait_borrowed_to_owned!(Mul, mul, Z, Q, Q);
141141
arithmetic_trait_mixed_borrowed_owned!(Mul, mul, Z, Q, Q);
142+
arithmetic_between_types!(Mul, mul, Z, Q, f32 f64);
142143

143144
impl Mul<&Zq> for &Z {
144145
type Output = Zq;
@@ -447,6 +448,26 @@ mod test_mul_between_z_and_q {
447448
use super::Z;
448449
use crate::rational::Q;
449450

451+
/// Ensuring multiplication between different types is available
452+
#[test]
453+
fn availability() {
454+
let a: Z = Z::from(42);
455+
let b: Q = Q::from((5, 7));
456+
457+
let _: Q = &a * &b;
458+
let _: Q = &a * b.clone();
459+
let _: Q = a.clone() * &b;
460+
let _: Q = a.clone() * b;
461+
let _: Q = &a * 0.5_f32;
462+
let _: Q = &a * 0.5_f64;
463+
let _: Q = a.clone() * 0.5_f32;
464+
let _: Q = a.clone() * 0.5_f64;
465+
let _: Q = 0.5_f32 * &a;
466+
let _: Q = 0.5_f64 * &a;
467+
let _: Q = 0.5_f32 * a.clone();
468+
let _: Q = 0.5_f64 * a.clone();
469+
}
470+
450471
/// Testing multiplication for [`Z`] and [`Q`]
451472
#[test]
452473
fn mul() {

src/integer/z/arithmetic/sub.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ impl Sub<&Q> for &Z {
141141

142142
arithmetic_trait_borrowed_to_owned!(Sub, sub, Z, Q, Q);
143143
arithmetic_trait_mixed_borrowed_owned!(Sub, sub, Z, Q, Q);
144+
arithmetic_between_types!(Sub, sub, Z, Q, f64 f32);
144145

145146
impl Sub<&Zq> for &Z {
146147
type Output = Zq;
@@ -397,6 +398,35 @@ mod test_sub_between_z_and_q {
397398
use super::Z;
398399
use crate::rational::Q;
399400

401+
/// Ensuring subtraction between different types is available
402+
#[test]
403+
fn availability() {
404+
let a: Z = Z::from(42);
405+
let b: Q = Q::from((5, 7));
406+
407+
let _: Q = &a - &b;
408+
let _: Q = &a - b.clone();
409+
let _: Q = a.clone() - &b;
410+
let _: Q = a.clone() - b;
411+
let _: Q = &a - 0.5_f32;
412+
let _: Q = &a - 0.5_f64;
413+
let _: Q = a.clone() - 0.5_f32;
414+
let _: Q = a.clone() - 0.5_f64;
415+
let _: Q = 0.5_f32 - &a;
416+
let _: Q = 0.5_f64 - &a;
417+
let _: Q = 0.5_f32 - a.clone();
418+
let _: Q = 0.5_f64 - a.clone();
419+
}
420+
421+
/// Ensures that division is performed the right way around
422+
#[test]
423+
fn order_retained() {
424+
let a = Z::from(4);
425+
426+
assert_eq!(2, &a - 2);
427+
assert_eq!(Q::from((-2, 1)), 2 - &a);
428+
}
429+
400430
/// Testing subtraction for [`Z`] and [`Q`]
401431
#[test]
402432
fn sub() {

src/macros/arithmetics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ macro_rules! arithmetic_between_types {
131131
paste::paste! {
132132
#[doc = "Documentation at [`" $type "::" $trait_function "`]."]
133133
fn $trait_function(self, other: &$other_type) -> Self::Output {
134-
self.$trait_function($type::from(*other))
134+
self.$trait_function($output_type::from(*other))
135135
}
136136
}
137137
}
@@ -145,7 +145,7 @@ macro_rules! arithmetic_between_types {
145145
paste::paste! {
146146
#[doc = "Documentation at [`" $type "::" $trait_function "`]."]
147147
fn $trait_function(self, other: &$type) -> Self::Output {
148-
$type::from(*self).$trait_function(other)
148+
$output_type::from(*self).$trait_function(other)
149149
}
150150
}
151151
}

0 commit comments

Comments
 (0)