mixed_num/
float_impl.rs

1use crate::*;
2use num::traits::float::FloatCore;
3
4use libm;
5
6mod f32_impl;
7pub use f32_impl::*;
8mod f64_impl;
9pub use f64_impl::*;
10
11macro_rules! impl_mixed_num_conversion{
12    ( $T1:ty, $T2:ty ) => {
13        impl MixedNumConversion<$T2> for $T1
14        {
15            #[inline(always)]
16            fn mixed_from_num( number:$T2 ) -> Self {
17                return number as Self;
18            }
19            #[inline(always)]
20            fn mixed_to_num( &self ) -> $T2 {
21                return *self as $T2;
22            }
23        }
24
25        impl MixedNumConversion<Cartesian<$T2>> for $T1
26        {
27            /// Extracts real part of self, including type cast to the target type.
28            /// 
29            /// ## Example
30            /// 
31            /// ```
32            /// use mixed_num::*;
33            /// use mixed_num::traits::*;
34            ///
35            /// let num = Cartesian::new(1f32,0f32);
36            /// 
37            /// let real_num:f64 = f64::mixed_from_num(num);
38            /// 
39            /// assert_eq!{ real_num, 1f64 };
40            /// ```
41            #[inline(always)]
42            fn mixed_from_num( number:Cartesian<$T2> ) -> Self {
43                return number.re as Self;
44            }
45            /// Casting real number to a complex, including type cast of T1 to T2 (Cartesian<T2>). 
46            ///  
47            /// ## Example
48            /// 
49            /// ```
50            /// use mixed_num::*;
51            /// use mixed_num::traits::*;
52            /// 
53            /// // Notice the support cor type cast as well as real/complex conversion. 
54            /// let num: Cartesian<f64> = 2f32.mixed_to_num();
55            /// 
56            /// assert_eq!{ num.to_string(), "2+0i" };
57            /// ```
58            #[inline(always)]
59            fn mixed_to_num( &self ) -> Cartesian<$T2> {
60                return Cartesian::new(*self as $T2, <$T2>::mixed_zero());
61            }
62        }
63    }
64}
65
66macro_rules! impl_mixed_num_for_primitive{
67    ( $T:ty ) => {
68
69        impl MixedNum for $T
70        {
71        }
72
73        impl_mixed_num_conversion!($T, f32);
74        impl_mixed_num_conversion!($T, f64);
75        
76        impl_mixed_num_conversion!($T, usize);
77        impl_mixed_num_conversion!($T, isize);
78        
79        impl_mixed_num_conversion!($T, u8);
80        impl_mixed_num_conversion!($T, u16);
81        impl_mixed_num_conversion!($T, u32);
82        impl_mixed_num_conversion!($T, u64);
83        impl_mixed_num_conversion!($T, u128);
84
85        impl_mixed_num_conversion!($T, i8);
86        impl_mixed_num_conversion!($T, i16);
87        impl_mixed_num_conversion!($T, i32);
88        impl_mixed_num_conversion!($T, i64);
89        impl_mixed_num_conversion!($T, i128);
90
91        impl MixedWrapPhase for $T
92        {
93            #[inline(always)]
94            fn mixed_wrap_phase(&self) -> Self {
95                return trigonometry::wrap_phase(*self);
96            }
97        }
98
99        impl MixedAbs for $T
100        {
101            #[inline(always)]
102            fn mixed_abs( &self ) -> Self {
103                return self.abs();
104            }
105        }
106
107        impl MixedPowi for $T
108        {
109            #[inline(always)]
110            fn mixed_powi( &self, exp: i32 ) -> Self {
111                return self.powi( exp );
112            }
113        }
114        
115        impl MixedOps for $T
116        {
117        }
118
119        impl MixedNumSigned for $T
120        {   
121        }
122        
123        impl MixedReal for $T
124        {
125            #[inline(always)]
126            fn mixed_max_value() -> Self {
127                return Self::max_value();
128            }
129            #[inline(always)]
130            fn mixed_min_value() -> Self {
131                return Self::min_value();
132            }
133            fn mixed_sign( &self) -> Self {
134                return trigonometry::sign(*self);
135            }
136            #[inline(always)]
137            fn mixed_is_positive( &self) -> bool {
138                return self.is_sign_positive();
139            }
140            #[inline(always)]
141            fn mixed_is_negative( &self) -> bool {
142                return self.is_sign_negative();
143            }
144        }
145
146        impl MixedZero for $T
147        {
148            #[inline(always)]
149            fn mixed_zero() -> Self {
150                return 0 as $T;
151            }
152        }
153
154        impl MixedOne for $T
155        {
156            #[inline(always)]
157            fn mixed_one() -> Self {
158                return 1 as $T;
159            }
160        }
161
162        impl MixedPi for $T
163        {
164            #[inline(always)]
165            fn mixed_pi() -> Self {
166                return 3.1415926535897932384626433832795028841971693993751058209749445923078164062 as $T;
167            }
168            #[inline(always)]
169            fn mixed_tau() -> Self {
170                return 6.2831853071795864769252867665590057683943387987502116419498891846156328124 as $T;
171            }
172        }
173
174        impl MixedConsts for $T
175        {
176        }
177
178        impl DbMag for $T
179        {
180            fn mixed_mag2db(&self) -> Self
181            {
182                return <$T>::mixed_from_num(20)*self.mixed_log10();
183            }
184            fn mixed_db2mag(&self) -> Self
185            {
186                let exponent: $T = *self/<$T>::mixed_from_num(20i32);
187                return exponent.mixed_exp10();
188            }
189        }
190
191        impl DbPow for $T
192        {
193            fn mixed_pow2db(&self) -> Self
194            {
195                return <$T>::mixed_from_num(10)*self.mixed_log10();
196            }
197            fn mixed_db2pow(&self) -> Self
198            {
199                let exponent: $T = *self/<$T>::mixed_from_num(10i32);
200                return exponent.mixed_exp10();
201            }
202        }
203    }
204}
205
206impl_mixed_num_for_primitive!(f32);
207impl_mixed_num_for_primitive!(f64);
208
209
210#[cfg(test)]
211mod tests {
212    // Note this useful idiom: importing names from outer (for mod tests) scope.
213    use super::*;
214
215    #[test]
216    fn pow2db() {
217        let numb = 2f32;
218        assert_eq!(numb.mixed_pow2db(), 3.0103002);
219    }
220
221    #[test]
222    fn db2pow() {
223        let numb = 10f32;
224        assert_eq!(numb.mixed_db2pow(), 10.0);
225    }
226
227    #[test]
228    fn mag2db() {
229        let numb = 2f32;
230        assert_eq!(numb.mixed_mag2db(), 6.0206003);
231    }
232
233    #[test]
234    fn db2mag() {
235        let numb = 10f32;
236        assert_eq!(numb.mixed_db2mag(), 3.1622777);
237    }
238}