1use crate::fmt::{Debug, Display, Formatter, LowerExp, Result, UpperExp};
2use crate::mem::MaybeUninit;
3use crate::num::flt2dec;
4use crate::num::fmt as numfmt;
5
6#[doc(hidden)]
7trait GeneralFormat: PartialOrd {
8 /// Determines if a value should use exponential based on its magnitude, given the precondition
9 /// that it will not be rounded any further before it is displayed.
10 fn already_rounded_value_should_use_exponential(&self) -> bool;
11}
12
13macro_rules! impl_general_format {
14 ($($t:ident)*) => {
15 $(impl GeneralFormat for $t {
16 fn already_rounded_value_should_use_exponential(&self) -> bool {
17 let abs = $t::abs_private(*self);
18 (abs != 0.0 && abs < 1e-4) || abs >= 1e+16
19 }
20 })*
21 }
22}
23
24impl_general_format! { f32 f64 }
25
26// Don't inline this so callers don't use the stack space this function
27// requires unless they have to.
28#[inline(never)]
29fn float_to_decimal_common_exact<T>(
30 fmt: &mut Formatter<'_>,
31 num: &T,
32 sign: flt2dec::Sign,
33 precision: usize,
34) -> Result
35where
36 T: flt2dec::DecodableFloat,
37{
38 let mut buf: [MaybeUninit<u8>; 1024] = MaybeUninit::uninit_array(); // enough for f32 and f64
39 let mut parts: [MaybeUninit<numfmt::Part<'_>>; 4] = MaybeUninit::uninit_array();
40 let formatted: Formatted<'_> = flt2dec::to_exact_fixed_str(
41 flt2dec::strategy::grisu::format_exact,
42 *num,
43 sign,
44 frac_digits:precision,
45 &mut buf,
46 &mut parts,
47 );
48 // SAFETY: `to_exact_fixed_str` and `format_exact` produce only ASCII characters.
49 unsafe { fmt.pad_formatted_parts(&formatted) }
50}
51
52// Don't inline this so callers that call both this and the above won't wind
53// up using the combined stack space of both functions in some cases.
54#[inline(never)]
55fn float_to_decimal_common_shortest<T>(
56 fmt: &mut Formatter<'_>,
57 num: &T,
58 sign: flt2dec::Sign,
59 precision: usize,
60) -> Result
61where
62 T: flt2dec::DecodableFloat,
63{
64 // enough for f32 and f64
65 let mut buf: [MaybeUninit<u8>; flt2dec::MAX_SIG_DIGITS] = MaybeUninit::uninit_array();
66 let mut parts: [MaybeUninit<numfmt::Part<'_>>; 4] = MaybeUninit::uninit_array();
67 let formatted: Formatted<'_> = flt2dec::to_shortest_str(
68 flt2dec::strategy::grisu::format_shortest,
69 *num,
70 sign,
71 frac_digits:precision,
72 &mut buf,
73 &mut parts,
74 );
75 // SAFETY: `to_shortest_str` and `format_shortest` produce only ASCII characters.
76 unsafe { fmt.pad_formatted_parts(&formatted) }
77}
78
79fn float_to_decimal_display<T>(fmt: &mut Formatter<'_>, num: &T) -> Result
80where
81 T: flt2dec::DecodableFloat,
82{
83 let force_sign: bool = fmt.sign_plus();
84 let sign: Sign = match force_sign {
85 false => flt2dec::Sign::Minus,
86 true => flt2dec::Sign::MinusPlus,
87 };
88
89 if let Some(precision: usize) = fmt.precision {
90 float_to_decimal_common_exact(fmt, num, sign, precision)
91 } else {
92 let min_precision: usize = 0;
93 float_to_decimal_common_shortest(fmt, num, sign, min_precision)
94 }
95}
96
97// Don't inline this so callers don't use the stack space this function
98// requires unless they have to.
99#[inline(never)]
100fn float_to_exponential_common_exact<T>(
101 fmt: &mut Formatter<'_>,
102 num: &T,
103 sign: flt2dec::Sign,
104 precision: usize,
105 upper: bool,
106) -> Result
107where
108 T: flt2dec::DecodableFloat,
109{
110 let mut buf: [MaybeUninit<u8>; 1024] = MaybeUninit::uninit_array(); // enough for f32 and f64
111 let mut parts: [MaybeUninit<numfmt::Part<'_>>; 6] = MaybeUninit::uninit_array();
112 let formatted: Formatted<'_> = flt2dec::to_exact_exp_str(
113 flt2dec::strategy::grisu::format_exact,
114 *num,
115 sign,
116 ndigits:precision,
117 upper,
118 &mut buf,
119 &mut parts,
120 );
121 // SAFETY: `to_exact_exp_str` and `format_exact` produce only ASCII characters.
122 unsafe { fmt.pad_formatted_parts(&formatted) }
123}
124
125// Don't inline this so callers that call both this and the above won't wind
126// up using the combined stack space of both functions in some cases.
127#[inline(never)]
128fn float_to_exponential_common_shortest<T>(
129 fmt: &mut Formatter<'_>,
130 num: &T,
131 sign: flt2dec::Sign,
132 upper: bool,
133) -> Result
134where
135 T: flt2dec::DecodableFloat,
136{
137 // enough for f32 and f64
138 let mut buf: [MaybeUninit<u8>; flt2dec::MAX_SIG_DIGITS] = MaybeUninit::uninit_array();
139 let mut parts: [MaybeUninit<numfmt::Part<'_>>; 6] = MaybeUninit::uninit_array();
140 let formatted: Formatted<'_> = flt2dec::to_shortest_exp_str(
141 flt2dec::strategy::grisu::format_shortest,
142 *num,
143 sign,
144 (0, 0),
145 upper,
146 &mut buf,
147 &mut parts,
148 );
149 // SAFETY: `to_shortest_exp_str` and `format_shortest` produce only ASCII characters.
150 unsafe { fmt.pad_formatted_parts(&formatted) }
151}
152
153// Common code of floating point LowerExp and UpperExp.
154fn float_to_exponential_common<T>(fmt: &mut Formatter<'_>, num: &T, upper: bool) -> Result
155where
156 T: flt2dec::DecodableFloat,
157{
158 let force_sign: bool = fmt.sign_plus();
159 let sign: Sign = match force_sign {
160 false => flt2dec::Sign::Minus,
161 true => flt2dec::Sign::MinusPlus,
162 };
163
164 if let Some(precision: usize) = fmt.precision {
165 // 1 integral digit + `precision` fractional digits = `precision + 1` total digits
166 float_to_exponential_common_exact(fmt, num, sign, precision:precision + 1, upper)
167 } else {
168 float_to_exponential_common_shortest(fmt, num, sign, upper)
169 }
170}
171
172fn float_to_general_debug<T>(fmt: &mut Formatter<'_>, num: &T) -> Result
173where
174 T: flt2dec::DecodableFloat + GeneralFormat,
175{
176 let force_sign: bool = fmt.sign_plus();
177 let sign: Sign = match force_sign {
178 false => flt2dec::Sign::Minus,
179 true => flt2dec::Sign::MinusPlus,
180 };
181
182 if let Some(precision: usize) = fmt.precision {
183 // this behavior of {:.PREC?} predates exponential formatting for {:?}
184 float_to_decimal_common_exact(fmt, num, sign, precision)
185 } else {
186 // since there is no precision, there will be no rounding
187 if num.already_rounded_value_should_use_exponential() {
188 let upper: bool = false;
189 float_to_exponential_common_shortest(fmt, num, sign, upper)
190 } else {
191 let min_precision: usize = 1;
192 float_to_decimal_common_shortest(fmt, num, sign, min_precision)
193 }
194 }
195}
196
197macro_rules! floating {
198 ($ty:ident) => {
199 #[stable(feature = "rust1", since = "1.0.0")]
200 impl Debug for $ty {
201 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
202 float_to_general_debug(fmt, self)
203 }
204 }
205
206 #[stable(feature = "rust1", since = "1.0.0")]
207 impl Display for $ty {
208 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
209 float_to_decimal_display(fmt, self)
210 }
211 }
212
213 #[stable(feature = "rust1", since = "1.0.0")]
214 impl LowerExp for $ty {
215 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
216 float_to_exponential_common(fmt, self, false)
217 }
218 }
219
220 #[stable(feature = "rust1", since = "1.0.0")]
221 impl UpperExp for $ty {
222 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
223 float_to_exponential_common(fmt, self, true)
224 }
225 }
226 };
227}
228
229floating! { f32 }
230floating! { f64 }
231