mixed_num/complex/cartesian_impl/
num_complex_impl.rs1use 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 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 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 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 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}