Fix handling of very large precision in fixed format

This commit is contained in:
Victor Zverovich 2021-11-27 08:23:05 -08:00
parent 201971e293
commit c472a27818
2 changed files with 21 additions and 4 deletions

View File

@ -2375,10 +2375,6 @@ FMT_HEADER_ONLY_CONSTEXPR20 int format_float(Float value, int precision,
const auto cached_pow = get_cached_power(
min_exp - (normalized.e + fp::num_significand_bits), cached_exp10);
normalized = normalized * cached_pow;
// Limit precision to the maximum possible number of significant digits in
// an IEEE754 double because we don't need to generate zeros.
const int max_double_digits = 767;
if (precision > max_double_digits) precision = max_double_digits;
gen_digits_handler handler{buf.data(), 0, precision, -cached_exp10, fixed};
if (grisu_gen_digits(normalized, 1, exp, handler) != digits::error &&
!is_constant_evaluated()) {
@ -2394,6 +2390,10 @@ FMT_HEADER_ONLY_CONSTEXPR20 int format_float(Float value, int precision,
auto f = fp();
bool is_predecessor_closer =
specs.binary32 ? f.assign(static_cast<float>(value)) : f.assign(value);
// Limit precision to the maximum possible number of significant digits in
// an IEEE754 double because we don't need to generate zeros.
const int max_double_digits = 767;
if (precision > max_double_digits) precision = max_double_digits;
format_dragon(f, is_predecessor_closer, precision, buf, exp);
}
if (!fixed && !specs.showpoint) {

View File

@ -872,6 +872,23 @@ TEST(format_test, precision) {
"117102665855668676818703956031062493194527159149245532930545654440112748"
"012970999954193198940908041656332452475714786901472678015935523861155013"
"480352649347201937902681071074917033322268447533357208324319361e-324");
EXPECT_EQ(
fmt::format("{:.1074f}", 1.1125369292536e-308),
"0.0000000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000111253692925360019747947051741965785554081512200979"
"355021686109411883779182127659725163430929750364498219730822952552570601"
"152163505899912777129583674906301179059298598412303893909188340988729019"
"014361467448914817838555156840459458527907308695109202499990850735085304"
"478476991912072201449236975063640913461919914396877093174125167509869762"
"482369631100360266123742648159508919592746619553246586039571522788247697"
"156360766271842991667238355464496455107749716934387136380536472531224398"
"559833794807213172371254492216255558078524900147957309382830827524104234"
"530961756787819847850302379672357738807808384667004752163416921762619527"
"462847642037420991432005657440259928195996762610375541867198059294212446"
"81962777939941034720757232455434770912461317493580281734466552734375");
std::string outputs[] = {
"-0X1.41FE3FFE71C9E000000000000000000000000000000000000000000000000000000"