@@ -2332,7 +2332,7 @@ template <typename Char, typename OutputIt, typename DecimalFP,
2332
2332
typename Grouping = digit_grouping<Char>>
2333
2333
FMT_CONSTEXPR20 auto do_write_float (OutputIt out, const DecimalFP& f,
2334
2334
const format_specs& specs, sign s,
2335
- locale_ref loc) -> OutputIt {
2335
+ int exp_upper, locale_ref loc) -> OutputIt {
2336
2336
auto significand = f.significand ;
2337
2337
int significand_size = get_significand_size (f);
2338
2338
const Char zero = static_cast <Char>(' 0' );
@@ -2348,7 +2348,7 @@ FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f,
2348
2348
if (specs.type () == presentation_type::fixed) return false ;
2349
2349
// Use the fixed notation if the exponent is in [exp_lower, exp_upper),
2350
2350
// e.g. 0.0001 instead of 1e-04. Otherwise use the exponent notation.
2351
- const int exp_lower = -4 , exp_upper = 16 ;
2351
+ const int exp_lower = -4 ;
2352
2352
return output_exp < exp_lower ||
2353
2353
output_exp >= (specs.precision > 0 ? specs.precision : exp_upper);
2354
2354
};
@@ -2451,12 +2451,13 @@ template <typename Char> class fallback_digit_grouping {
2451
2451
template <typename Char, typename OutputIt, typename DecimalFP>
2452
2452
FMT_CONSTEXPR20 auto write_float (OutputIt out, const DecimalFP& f,
2453
2453
const format_specs& specs, sign s,
2454
- locale_ref loc) -> OutputIt {
2454
+ int exp_upper, locale_ref loc) -> OutputIt {
2455
2455
if (is_constant_evaluated ()) {
2456
2456
return do_write_float<Char, OutputIt, DecimalFP,
2457
- fallback_digit_grouping<Char>>(out, f, specs, s, loc);
2457
+ fallback_digit_grouping<Char>>(out, f, specs, s,
2458
+ exp_upper, loc);
2458
2459
} else {
2459
- return do_write_float<Char>(out, f, specs, s, loc);
2460
+ return do_write_float<Char>(out, f, specs, s, exp_upper, loc);
2460
2461
}
2461
2462
}
2462
2463
@@ -3288,6 +3289,14 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision,
3288
3289
return exp ;
3289
3290
}
3290
3291
3292
+ // Numbers with exponents greater or equal to the returned value will use
3293
+ // the exponential notation.
3294
+ template <typename T> constexpr auto exp_upper () -> int {
3295
+ return std::numeric_limits<T>::digits10 != 0
3296
+ ? min_of (16 , std::numeric_limits<T>::digits10 + 1 )
3297
+ : 16 ;
3298
+ }
3299
+
3291
3300
template <typename Char, typename OutputIt, typename T>
3292
3301
FMT_CONSTEXPR20 auto write_float (OutputIt out, T value, format_specs specs,
3293
3302
locale_ref loc) -> OutputIt {
@@ -3303,6 +3312,7 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs,
3303
3312
if (specs.width != 0 ) --specs.width ;
3304
3313
}
3305
3314
3315
+ constexpr int exp_upper = detail::exp_upper<T>();
3306
3316
int precision = specs.precision ;
3307
3317
if (precision < 0 ) {
3308
3318
if (specs.type () != presentation_type::none) {
@@ -3311,7 +3321,7 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs,
3311
3321
// Use Dragonbox for the shortest format.
3312
3322
using floaty = conditional_t <sizeof (T) >= sizeof (double ), double , float >;
3313
3323
auto dec = dragonbox::to_decimal (static_cast <floaty>(value));
3314
- return write_float<Char>(out, dec, specs, s, loc);
3324
+ return write_float<Char>(out, dec, specs, s, exp_upper, loc);
3315
3325
}
3316
3326
}
3317
3327
@@ -3339,7 +3349,7 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs,
3339
3349
3340
3350
specs.precision = precision;
3341
3351
auto f = big_decimal_fp{buffer.data (), static_cast <int >(buffer.size ()), exp };
3342
- return write_float<Char>(out, f, specs, s, loc);
3352
+ return write_float<Char>(out, f, specs, s, exp_upper, loc);
3343
3353
}
3344
3354
3345
3355
template <typename Char, typename OutputIt, typename T,
@@ -3366,7 +3376,7 @@ FMT_CONSTEXPR20 auto write(OutputIt out, T value) -> OutputIt {
3366
3376
return write_nonfinite<Char>(out, std::isnan (value), specs, s);
3367
3377
3368
3378
auto dec = dragonbox::to_decimal (static_cast <floaty>(value));
3369
- return write_float<Char>(out, dec, specs, s, {});
3379
+ return write_float<Char>(out, dec, specs, s, exp_upper<T>(), {});
3370
3380
}
3371
3381
3372
3382
template <typename Char, typename OutputIt, typename T,
0 commit comments