Skip to content

Commit 5fa88c8

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

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

include/fmt/format.h

+16-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,12 @@ 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 min_of(15, std::numeric_limits<T>::digits10) + 1;
3296+
}
3297+
32913298
template <typename Char, typename OutputIt, typename T>
32923299
FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs,
32933300
locale_ref loc) -> OutputIt {
@@ -3303,6 +3310,7 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs,
33033310
if (specs.width != 0) --specs.width;
33043311
}
33053312

3313+
constexpr int exp_upper = detail::exp_upper<T>();
33063314
int precision = specs.precision;
33073315
if (precision < 0) {
33083316
if (specs.type() != presentation_type::none) {
@@ -3311,7 +3319,7 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs,
33113319
// Use Dragonbox for the shortest format.
33123320
using floaty = conditional_t<sizeof(T) >= sizeof(double), double, float>;
33133321
auto dec = dragonbox::to_decimal(static_cast<floaty>(value));
3314-
return write_float<Char>(out, dec, specs, s, loc);
3322+
return write_float<Char>(out, dec, specs, s, exp_upper, loc);
33153323
}
33163324
}
33173325

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

33403348
specs.precision = precision;
33413349
auto f = big_decimal_fp{buffer.data(), static_cast<int>(buffer.size()), exp};
3342-
return write_float<Char>(out, f, specs, s, loc);
3350+
return write_float<Char>(out, f, specs, s, exp_upper, loc);
33433351
}
33443352

33453353
template <typename Char, typename OutputIt, typename T,
@@ -3366,7 +3374,7 @@ FMT_CONSTEXPR20 auto write(OutputIt out, T value) -> OutputIt {
33663374
return write_nonfinite<Char>(out, std::isnan(value), specs, s);
33673375

33683376
auto dec = dragonbox::to_decimal(static_cast<floaty>(value));
3369-
return write_float<Char>(out, dec, specs, s, {});
3377+
return write_float<Char>(out, dec, specs, s, exp_upper<T>(), {});
33703378
}
33713379

33723380
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)