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