Skip to content

Ignore zero-padding for non-finite floating points - #2310

Merged
vitaut merged 5 commits into
fmtlib:masterfrom
Liedtke:non_finite_zero_fill
May 27, 2021
Merged

vitaut merged 5 commits into
fmtlib:masterfrom
Liedtke:non_finite_zero_fill

Conversation

@Liedtke

@Liedtke Liedtke commented May 23, 2021

Copy link
Copy Markdown
Contributor

Fixes #2305.

Is there a better way to check for the 0-padding-option than what I wrote?

@vitaut vitaut left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

Comment thread test/format-test.cc Outdated
if (std::signbit(-nan)) {
EXPECT_EQ("-nan", fmt::format("{}", -nan));
else
EXPECT_EQ("-nan", fmt::format("{:+06}", -nan));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be " -nan" i.e. the width should still apply. Same elsewhere.

Comment thread include/fmt/format.h Outdated
Comment on lines +1594 to +1602
return write_padded(out, specs, size, [=](reserve_iterator<OutputIt> it) {
auto copy_it = [=](reserve_iterator<OutputIt> it) {
if (sign) *it++ = static_cast<Char>(data::signs[sign]);
return copy_str<Char>(str, str + str_size, it);
});
};
// no '0'-padding applied for non-finite values
const bool is_zero_fill =
specs.fill.size() == 1 && *specs.fill.data() == static_cast<Char>('0');
return is_zero_fill ? base_iterator(out, copy_it(reserve(out, size)))
: write_padded<align::right>(out, specs, size, copy_it);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest copying specs and changing fill from 0 to (space) instead.

Comment thread test/format-test.cc Outdated
Comment on lines +1300 to +1301
// '0'-fill option sets alignment to numeric overwriting any user-provided
// alignment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the desired behavior? Currently we lose the originally provided alignment because it seems to get replaced by align::numeric when using 0-padding.

@vitaut vitaut May 26, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we should preserve the original alignment. I think it can be dome by changing

specs_.align = align::numeric;

to something like

if (specs_.align == align::none) specs_.align = align::numeric; 

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that works!

@vitaut vitaut left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two nits, otherwise LGTM.

Comment thread include/fmt/format.h Outdated
constexpr size_t str_size = 3;
auto sign = fspecs.sign;
auto size = str_size + (sign ? 1 : 0);
// replace '0'-padding with space for non-finite values

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: please make this a proper sentence:

// Replace '0'-padding with space for non-finite values.

Comment thread test/format-test.cc
else
EXPECT_EQ(" -nan", fmt::format("{:+06}", -nan));
} else
fmt::print("Warning: compiler doesn't handle negative NaN correctly");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: please wrap the else statement in {} for consistency with if:

} else {
  fmt::print("Warning: compiler doesn't handle negative NaN correctly");
}

@vitaut
vitaut merged commit a70a4ae into fmtlib:master May 27, 2021
@vitaut

vitaut commented May 27, 2021

Copy link
Copy Markdown
Contributor

Thank you!

@Liedtke
Liedtke deleted the non_finite_zero_fill branch May 27, 2021 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Numeric zero fill is applied to inf/nan

2 participants