argmin/core/math/
div.rs

1// Copyright 2018-2020 argmin developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use crate::core::math::ArgminDiv;
9use num_complex::Complex;
10
11macro_rules! make_div {
12    ($t:ty) => {
13        impl ArgminDiv<$t, $t> for $t {
14            #[inline]
15            fn div(&self, other: &$t) -> $t {
16                self / other
17            }
18        }
19    };
20}
21
22make_div!(isize);
23make_div!(usize);
24make_div!(i8);
25make_div!(u8);
26make_div!(i16);
27make_div!(u16);
28make_div!(i32);
29make_div!(u32);
30make_div!(i64);
31make_div!(u64);
32make_div!(f32);
33make_div!(f64);
34make_div!(Complex<isize>);
35make_div!(Complex<usize>);
36make_div!(Complex<i8>);
37make_div!(Complex<u8>);
38make_div!(Complex<i16>);
39make_div!(Complex<u16>);
40make_div!(Complex<i32>);
41make_div!(Complex<u32>);
42make_div!(Complex<i64>);
43make_div!(Complex<u64>);
44make_div!(Complex<f32>);
45make_div!(Complex<f64>);
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50    use paste::item;
51
52    macro_rules! make_test {
53        ($t:ty) => {
54            item! {
55                #[test]
56                fn [<test_div_ $t>]() {
57                    let a = 84 as $t;
58                    let b = 2 as $t;
59                    let res = <$t as ArgminDiv<$t, $t>>::div(&a, &b);
60                    assert!(((42 as $t - res) as f64).abs() < std::f64::EPSILON);
61                }
62            }
63        };
64    }
65
66    make_test!(isize);
67    make_test!(usize);
68    make_test!(i8);
69    make_test!(u8);
70    make_test!(i16);
71    make_test!(u16);
72    make_test!(i32);
73    make_test!(u32);
74    make_test!(i64);
75    make_test!(u64);
76    make_test!(f32);
77    make_test!(f64);
78}