mixed_num/complex/cartesian_impl/
num_complex_impl.rs

1// This file is an edited version of the lib.rs file of the num-complex crate.
2// The lisence of this particular code section is included in the LISENCE file.
3
4use crate::traits::*;
5use crate::Cartesian;
6use core::fmt;
7
8
9macro_rules! write_complex {
10    ($f:ident, $t:expr, $prefix:expr, $re:expr, $im:expr) => {{
11        let abs_re = if $re < T::mixed_zero() {
12            T::mixed_zero() - $re.clone()
13        } else {
14            $re.clone()
15        };
16        let abs_im = if $im < T::mixed_zero() {
17            T::mixed_zero() - $im.clone()
18        } else {
19            $im.clone()
20        };
21
22        return if let Some(prec) = $f.precision() {
23            fmt_re_im(
24                $f,
25                $re < T::mixed_zero(),
26                $im < T::mixed_zero(),
27                format_args!(concat!("{:.1$", $t, "}"), abs_re, prec),
28                format_args!(concat!("{:.1$", $t, "}"), abs_im, prec),
29            )
30        } else {
31            fmt_re_im(
32                $f,
33                $re < T::mixed_zero(),
34                $im < T::mixed_zero(),
35                format_args!(concat!("{:", $t, "}"), abs_re),
36                format_args!(concat!("{:", $t, "}"), abs_im),
37            )
38        };
39
40        fn fmt_re_im(
41            f: &mut core::fmt::Formatter<'_>,
42            re_neg: bool,
43            im_neg: bool,
44            real: core::fmt::Arguments<'_>,
45            imag: core::fmt::Arguments<'_>,
46        ) -> core::fmt::Result {
47            let prefix = if f.alternate() { $prefix } else { "" };
48            let sign = if re_neg {
49                "-"
50            } else if f.sign_plus() {
51                "+"
52            } else {
53                ""
54            };
55
56            if im_neg {
57                fmt_complex(
58                    f,
59                    format_args!(
60                        "{}{pre}{re}-{pre}{im}i",
61                        sign,
62                        re = real,
63                        im = imag,
64                        pre = prefix
65                    ),
66                )
67            } else {
68                fmt_complex(
69                    f,
70                    format_args!(
71                        "{}{pre}{re}+{pre}{im}i",
72                        sign,
73                        re = real,
74                        im = imag,
75                        pre = prefix
76                    ),
77                )
78            }
79        }
80
81        #[cfg(feature = "std")]
82        // Currently, we can only apply width using an intermediate `String` (and thus `std`)
83        fn fmt_complex(f: &mut fmt::Formatter<'_>, complex: fmt::Arguments<'_>) -> fmt::Result {
84            use std::string::ToString;
85            if let Some(width) = f.width() {
86                write!(f, "{0: >1$}", complex.to_string(), width)
87            } else {
88                write!(f, "{}", complex)
89            }
90        }
91
92        #[cfg(not(feature = "std"))]
93        fn fmt_complex(f: &mut core::fmt::Formatter<'_>, complex: core::fmt::Arguments<'_>) -> core::fmt::Result {
94            write!(f, "{}", complex)
95        }
96    }};
97}
98
99impl<T> fmt::Display for Cartesian<T>
100where
101    T: fmt::Display + PartialOrd + Clone + MixedZero + MixedNum + core::ops::Sub<Output = T>,
102{
103    /// ## Example
104    /// 
105    /// ```
106    /// use mixed_num::*;
107    /// use mixed_num::traits::*;
108    /// 
109    /// let mut c_num = Cartesian::new(-2f32,4f32);
110    /// assert_eq!{ c_num.to_string(), "-2+4i" };
111    /// let mut c_num = Cartesian::new(2f32,-4f32);
112    /// assert_eq!{ c_num.to_string(), "2-4i" };
113    /// ```
114    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115        write_complex!(f, "", "", self.re, self.im)
116    }
117}
118
119impl<T> fmt::LowerExp for Cartesian<T>
120where
121    T: fmt::LowerExp + PartialOrd + Clone + MixedZero + MixedNum + core::ops::Sub<Output = T>,
122{
123    /// ## Example
124    /// 
125    /// ```
126    /// use mixed_num::*;
127    /// use mixed_num::traits::*;
128    /// 
129    /// let mut c_num = Cartesian::new(2e9f32,-4f32);
130    /// assert_eq!{ format!("{:e}", c_num), "2e9-4e0i" };
131    /// ```
132    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133        write_complex!(f, "e", "", self.re, self.im)
134    }
135}
136
137impl<T> fmt::UpperExp for Cartesian<T>
138where
139    T: fmt::UpperExp + PartialOrd + Clone + MixedZero + MixedNum + core::ops::Sub<Output = T>,
140{
141    /// ## Example
142    /// 
143    /// ```
144    /// use mixed_num::*;
145    /// use mixed_num::traits::*;
146    /// 
147    /// let mut c_num = Cartesian::new(2e9f32,-4f32);
148    /// assert_eq!{ format!("{:E}", c_num), "2E9-4E0i" };
149    /// ```
150    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151        write_complex!(f, "E", "", self.re, self.im)
152    }
153}
154
155impl<T> fmt::LowerHex for Cartesian<T>
156where
157    T: fmt::LowerHex + PartialOrd + Clone + MixedZero + MixedNum + core::ops::Sub<Output = T>,
158{
159    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
160        write_complex!(f, "x", "0x", self.re, self.im)
161    }
162}
163
164impl<T> fmt::UpperHex for Cartesian<T>
165where
166    T: fmt::UpperHex + PartialOrd + Clone + MixedZero + MixedNum + core::ops::Sub<Output = T>,
167{
168    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169        write_complex!(f, "X", "0x", self.re, self.im)
170    }
171}
172
173impl<T> fmt::Octal for Cartesian<T>
174where
175    T: fmt::Octal + PartialOrd + Clone + MixedZero + MixedNum + core::ops::Sub<Output = T>,
176{
177    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
178        write_complex!(f, "o", "0o", self.re, self.im)
179    }
180}
181
182impl<T> fmt::Binary for Cartesian<T>
183where
184    T: fmt::Binary + PartialOrd + Clone + MixedZero + MixedNum + core::ops::Sub<Output = T>,
185{
186    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
187        write_complex!(f, "b", "0b", self.re, self.im)
188    }
189}