refined_float/
macros.rs

1#[macro_export]
2macro_rules! impl_arithmetic_trait {
3    ($fl_ty: ty, $trait_0: tt, $trait_1: tt, $fn_0: tt, $fn_1: tt) => {
4        impl core::ops::$trait_0 for $fl_ty {
5            type Output = Self;
6
7            #[inline]
8            fn $fn_0(self, rhs: $fl_ty) -> Self {
9                Self(self.0.$fn_0(rhs.0))
10            }
11        }
12
13        impl core::ops::$trait_1 for $fl_ty {
14            #[inline]
15            fn $fn_1(&mut self, rhs: $fl_ty) {
16                self.0.$fn_1(rhs.0)
17            }
18        }
19    };
20}
21
22#[macro_export]
23macro_rules! impl_sum_product {
24    ($fl_ty: ty) => {
25        impl core::iter::Sum for $fl_ty {
26            fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
27                Self(iter.map(|x| x.0).sum())
28            }
29        }
30
31        impl core::iter::Product for $fl_ty {
32            fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
33                Self(iter.map(|x| x.0).product())
34            }
35        }
36    };
37}
38
39#[macro_export]
40macro_rules! impl_neg_trait {
41    ($fl_ty: ty) => {
42        impl core::ops::Neg for $fl_ty {
43            type Output = Self;
44
45            #[inline]
46            fn neg(self) -> Self::Output {
47                Self(self.0.neg())
48            }
49        }
50    };
51}
52
53#[macro_export]
54macro_rules! impl_debug_display_trait {
55    ($fl_ty: ty) => {
56        impl core::fmt::Debug for $fl_ty {
57            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58                core::fmt::Debug::fmt(&self.0, f)
59            }
60        }
61
62        impl core::fmt::Display for $fl_ty {
63            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64                core::fmt::Display::fmt(&self.0, f)
65            }
66        }
67    };
68}
69
70#[macro_export]
71macro_rules! impl_complex_fns {
72    ($fl_ty: ty, $inner_ty: ty, $fn: tt, $std_fn: tt) => {
73        impl $fl_ty {
74            #[inline]
75            pub fn $fn(self) -> Self {
76                #[cfg(feature = "std")]
77                return Self(self.0.$std_fn());
78                #[cfg(all(not(feature = "std"), feature = "libm"))]
79                return Self(libm::Libm::<$inner_ty>::$fn(self.0));
80            }
81        }
82    };
83}
84
85#[macro_export]
86macro_rules! impl_float_const {
87    ($fl_ty: tt,$name:tt, $value: expr) => {
88        impl $fl_ty {
89            pub const $name: $fl_ty = $fl_ty($value);
90        }
91    };
92}
93
94macro_rules! impl_approx_traits {
95    ($fl_ty: ty, $inner_ty: ty) => {
96        #[cfg(feature = "approx")]
97        mod impl_approx {
98            impl approx::AbsDiffEq for $fl_ty {
99                type Epsilon = Self;
100
101                fn default_epsilon() -> Self::Epsilon {
102                    Self(<$inner_ty as approx::AbsDiffEq>::default_epsilon())
103                }
104
105                fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
106                    approx::AbsDiffEq::abs_diff_eq(&self.0, &other.0, epsilon.0)
107                }
108            }
109
110            impl approx::RelativeEq for $fl_ty {
111                fn default_max_relative() -> Self::Epsilon {
112                    Self(<$inner_ty as approx::RelativeEq>::default_max_relative())
113                }
114
115                fn relative_eq(
116                    &self,
117                    other: &Self,
118                    epsilon: Self::Epsilon,
119                    max_relative: Self::Epsilon,
120                ) -> bool {
121                    approx::RelativeEq::relative_eq(&self.0, &other.0, epsilon.0, max_relative.0)
122                }
123            }
124
125            impl approx::UlpsEq for $fl_ty {
126                fn default_max_ulps() -> u32 {
127                    <$inner_ty as approx::UlpsEq>::default_max_ulps()
128                }
129
130                fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
131                    approx::UlpsEq::ulps_eq(&self.0, &other.0, epsilon.0, max_ulps)
132                }
133            }
134        }
135    };
136}
137
138pub(crate) use impl_approx_traits;
139pub(crate) use impl_arithmetic_trait;
140pub(crate) use impl_complex_fns;
141pub(crate) use impl_debug_display_trait;
142pub(crate) use impl_float_const;
143pub(crate) use impl_neg_trait;
144pub(crate) use impl_sum_product;