diff --git a/include/fmt/format.h b/include/fmt/format.h index 4466b4f4d24b..287e71631e35 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2332,7 +2332,7 @@ template > FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f, const format_specs& specs, sign s, - locale_ref loc) -> OutputIt { + int exp_upper, locale_ref loc) -> OutputIt { auto significand = f.significand; int significand_size = get_significand_size(f); const Char zero = static_cast('0'); @@ -2348,7 +2348,7 @@ FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f, if (specs.type() == presentation_type::fixed) return false; // Use the fixed notation if the exponent is in [exp_lower, exp_upper), // e.g. 0.0001 instead of 1e-04. Otherwise use the exponent notation. - const int exp_lower = -4, exp_upper = 16; + const int exp_lower = -4; return output_exp < exp_lower || output_exp >= (specs.precision > 0 ? specs.precision : exp_upper); }; @@ -2451,12 +2451,13 @@ template class fallback_digit_grouping { template FMT_CONSTEXPR20 auto write_float(OutputIt out, const DecimalFP& f, const format_specs& specs, sign s, - locale_ref loc) -> OutputIt { + int exp_upper, locale_ref loc) -> OutputIt { if (is_constant_evaluated()) { return do_write_float>(out, f, specs, s, loc); + fallback_digit_grouping>(out, f, specs, s, + exp_upper, loc); } else { - return do_write_float(out, f, specs, s, loc); + return do_write_float(out, f, specs, s, exp_upper, loc); } } @@ -3288,6 +3289,14 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision, return exp; } +// Numbers with exponents greater or equal to the returned value will use +// the exponential notation. +template constexpr auto exp_upper() -> int { + return std::numeric_limits::digits10 != 0 + ? min_of(16, std::numeric_limits::digits10 + 1) + : 16; +} + template FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs, locale_ref loc) -> OutputIt { @@ -3303,6 +3312,7 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs, if (specs.width != 0) --specs.width; } + constexpr int exp_upper = detail::exp_upper(); int precision = specs.precision; if (precision < 0) { if (specs.type() != presentation_type::none) { @@ -3311,7 +3321,7 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs, // Use Dragonbox for the shortest format. using floaty = conditional_t= sizeof(double), double, float>; auto dec = dragonbox::to_decimal(static_cast(value)); - return write_float(out, dec, specs, s, loc); + return write_float(out, dec, specs, s, exp_upper, loc); } } @@ -3339,7 +3349,7 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs, specs.precision = precision; auto f = big_decimal_fp{buffer.data(), static_cast(buffer.size()), exp}; - return write_float(out, f, specs, s, loc); + return write_float(out, f, specs, s, exp_upper, loc); } template OutputIt { return write_nonfinite(out, std::isnan(value), specs, s); auto dec = dragonbox::to_decimal(static_cast(value)); - return write_float(out, dec, specs, s, {}); + return write_float(out, dec, specs, s, exp_upper(), {}); } template struct numeric_limits { // is_iec559 is true for double-double in libstdc++. static constexpr bool is_iec559 = true; static constexpr int digits = 106; + static constexpr int digits10 = 33; }; template <> struct is_floating_point : std::true_type {}; @@ -341,7 +342,7 @@ TEST(format_impl_test, write_dragon_even) { auto s = std::string(); fmt::detail::write(std::back_inserter(s), slow_float(33554450.0f), {}); // Specializing is_floating_point is broken in MSVC. - if (!FMT_MSC_VERSION) EXPECT_EQ(s, "33554450"); + if (!FMT_MSC_VERSION) EXPECT_EQ(s, "3.355445e+07"); } #if defined(_WIN32) && !defined(FMT_USE_WRITE_CONSOLE) diff --git a/test/format-test.cc b/test/format-test.cc index 34c7f44eda88..c16f895bed29 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1068,7 +1068,8 @@ TEST(format_test, precision) { EXPECT_EQ(fmt::format("{:#.0f}", 123.0), "123."); EXPECT_EQ(fmt::format("{:.02f}", 1.234), "1.23"); EXPECT_EQ(fmt::format("{:.1g}", 0.001), "0.001"); - EXPECT_EQ(fmt::format("{}", 1019666432.0f), "1019666400"); + EXPECT_EQ(fmt::format("{}", 123456789.0f), "1.2345679e+08"); + EXPECT_EQ(fmt::format("{}", 1019666432.0f), "1.0196664e+09"); EXPECT_EQ(fmt::format("{:.0e}", 9.5), "1e+01"); EXPECT_EQ(fmt::format("{:.1e}", 1e-34), "1.0e-34");