Skip to content

Commit 52eeeb5

Browse files
committed
Make exponent threshold depend on representation (#3649)
1 parent 9cf9f38 commit 52eeeb5

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

include/fmt/format.h

+18-8
Original file line numberDiff line numberDiff line change
@@ -2332,7 +2332,7 @@ template <typename Char, typename OutputIt, typename DecimalFP,
23322332
typename Grouping = digit_grouping<Char>>
23332333
FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f,
23342334
const format_specs& specs, sign s,
2335-
locale_ref loc) -> OutputIt {
2335+
int exp_upper, locale_ref loc) -> OutputIt {
23362336
auto significand = f.significand;
23372337
int significand_size = get_significand_size(f);
23382338
const Char zero = static_cast<Char>('0');
@@ -2348,7 +2348,7 @@ FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f,
23482348
if (specs.type() == presentation_type::fixed) return false;
23492349
// Use the fixed notation if the exponent is in [exp_lower, exp_upper),
23502350
// 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;
23522352
return output_exp < exp_lower ||
23532353
output_exp >= (specs.precision > 0 ? specs.precision : exp_upper);
23542354
};
@@ -2451,12 +2451,13 @@ template <typename Char> class fallback_digit_grouping {
24512451
template <typename Char, typename OutputIt, typename DecimalFP>
24522452
FMT_CONSTEXPR20 auto write_float(OutputIt out, const DecimalFP& f,
24532453
const format_specs& specs, sign s,
2454-
locale_ref loc) -> OutputIt {
2454+
int exp_upper, locale_ref loc) -> OutputIt {
24552455
if (is_constant_evaluated()) {
24562456
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);
24582459
} 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);
24602461
}
24612462
}
24622463

@@ -3288,6 +3289,14 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision,
32883289
return exp;
32893290
}
32903291

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+
32913300
template <typename Char, typename OutputIt, typename T>
32923301
FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs,
32933302
locale_ref loc) -> OutputIt {
@@ -3303,6 +3312,7 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs,
33033312
if (specs.width != 0) --specs.width;
33043313
}
33053314

3315+
constexpr int exp_upper = detail::exp_upper<T>();
33063316
int precision = specs.precision;
33073317
if (precision < 0) {
33083318
if (specs.type() != presentation_type::none) {
@@ -3311,7 +3321,7 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs,
33113321
// Use Dragonbox for the shortest format.
33123322
using floaty = conditional_t<sizeof(T) >= sizeof(double), double, float>;
33133323
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);
33153325
}
33163326
}
33173327

@@ -3339,7 +3349,7 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs,
33393349

33403350
specs.precision = precision;
33413351
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);
33433353
}
33443354

33453355
template <typename Char, typename OutputIt, typename T,
@@ -3366,7 +3376,7 @@ FMT_CONSTEXPR20 auto write(OutputIt out, T value) -> OutputIt {
33663376
return write_nonfinite<Char>(out, std::isnan(value), specs, s);
33673377

33683378
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>(), {});
33703380
}
33713381

33723382
template <typename Char, typename OutputIt, typename T,

test/format-impl-test.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ template <> struct numeric_limits<double_double> {
312312
// is_iec559 is true for double-double in libstdc++.
313313
static constexpr bool is_iec559 = true;
314314
static constexpr int digits = 106;
315+
static constexpr int digits10 = 33;
315316
};
316317

317318
template <> struct is_floating_point<slow_float> : std::true_type {};
@@ -341,7 +342,7 @@ TEST(format_impl_test, write_dragon_even) {
341342
auto s = std::string();
342343
fmt::detail::write<char>(std::back_inserter(s), slow_float(33554450.0f), {});
343344
// Specializing is_floating_point is broken in MSVC.
344-
if (!FMT_MSC_VERSION) EXPECT_EQ(s, "33554450");
345+
if (!FMT_MSC_VERSION) EXPECT_EQ(s, "3.355445e+07");
345346
}
346347

347348
#if defined(_WIN32) && !defined(FMT_USE_WRITE_CONSOLE)

test/format-test.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,8 @@ TEST(format_test, precision) {
10681068
EXPECT_EQ(fmt::format("{:#.0f}", 123.0), "123.");
10691069
EXPECT_EQ(fmt::format("{:.02f}", 1.234), "1.23");
10701070
EXPECT_EQ(fmt::format("{:.1g}", 0.001), "0.001");
1071-
EXPECT_EQ(fmt::format("{}", 1019666432.0f), "1019666400");
1071+
EXPECT_EQ(fmt::format("{}", 123456789.0f), "1.2345679e+08");
1072+
EXPECT_EQ(fmt::format("{}", 1019666432.0f), "1.0196664e+09");
10721073
EXPECT_EQ(fmt::format("{:.0e}", 9.5), "1e+01");
10731074
EXPECT_EQ(fmt::format("{:.1e}", 1e-34), "1.0e-34");
10741075

0 commit comments

Comments
 (0)