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;
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.
10 fn already_rounded_value_should_use_exponential(&self) -> bool;
13 macro_rules! impl_general_format {
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
24 impl_general_format! { f32 f64 }
26 // Don't inline this so callers don't use the stack space this function
27 // requires unless they have to.
29 fn float_to_decimal_common_exact<T>(
30 fmt: &mut Formatter<'_>,
36 T: flt2dec::DecodableFloat,
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,
48 fmt.pad_formatted_parts(&formatted)
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.
54 fn float_to_decimal_common_shortest<T>(
55 fmt: &mut Formatter<'_>,
61 T: flt2dec::DecodableFloat,
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,
74 fmt.pad_formatted_parts(&formatted)
77 fn float_to_decimal_display<T>(fmt: &mut Formatter<'_>, num: &T) -> Result
79 T: flt2dec::DecodableFloat,
81 let force_sign = fmt.sign_plus();
82 let sign = match force_sign {
83 false => flt2dec::Sign::Minus,
84 true => flt2dec::Sign::MinusPlus,
87 if let Some(precision) = fmt.precision {
88 float_to_decimal_common_exact(fmt, num, sign, precision)
90 let min_precision = 0;
91 float_to_decimal_common_shortest(fmt, num, sign, min_precision)
95 // Don't inline this so callers don't use the stack space this function
96 // requires unless they have to.
98 fn float_to_exponential_common_exact<T>(
99 fmt: &mut Formatter<'_>,
106 T: flt2dec::DecodableFloat,
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,
119 fmt.pad_formatted_parts(&formatted)
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.
125 fn float_to_exponential_common_shortest<T>(
126 fmt: &mut Formatter<'_>,
132 T: flt2dec::DecodableFloat,
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,
146 fmt.pad_formatted_parts(&formatted)
149 // Common code of floating point LowerExp and UpperExp.
150 fn float_to_exponential_common<T>(fmt: &mut Formatter<'_>, num: &T, upper: bool) -> Result
152 T: flt2dec::DecodableFloat,
154 let force_sign = fmt.sign_plus();
155 let sign = match force_sign {
156 false => flt2dec::Sign::Minus,
157 true => flt2dec::Sign::MinusPlus,
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)
164 float_to_exponential_common_shortest(fmt, num, sign, upper)
168 fn float_to_general_debug<T>(fmt: &mut Formatter<'_>, num: &T) -> Result
170 T: flt2dec::DecodableFloat + GeneralFormat,
172 let force_sign = fmt.sign_plus();
173 let sign = match force_sign {
174 false => flt2dec::Sign::Minus,
175 true => flt2dec::Sign::MinusPlus,
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)
182 // since there is no precision, there will be no rounding
183 if num.already_rounded_value_should_use_exponential() {
185 float_to_exponential_common_shortest(fmt, num, sign, upper)
187 let min_precision = 1;
188 float_to_decimal_common_shortest(fmt, num, sign, min_precision)
193 macro_rules! floating {
195 #[stable(feature = "rust1", since = "1.0.0")]
197 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
198 float_to_general_debug(fmt, self)
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)
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)
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)