Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
May 27, 2021

Conversation

Liedtke
Copy link
Contributor

@Liedtke Liedtke commented May 23, 2021

Fixes #2305.

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

Copy link
Contributor

@vitaut vitaut left a comment

Choose a reason for hiding this comment

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

Thanks for the PR.

EXPECT_EQ("-nan", fmt::format("{}", -nan));
else
EXPECT_EQ("-nan", fmt::format("{:+06}", -nan));
Copy link
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 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
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 on lines 1300 to 1301
// '0'-fill option sets alignment to numeric overwriting any user-provided
// alignment
Copy link
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.

Copy link
Contributor

@vitaut vitaut May 26, 2021

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
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!

Copy link
Contributor

@vitaut vitaut left a comment

Choose a reason for hiding this comment

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

Two nits, otherwise LGTM.

const float_specs& fspecs) {
auto str =
isinf ? (fspecs.upper ? "INF" : "inf") : (fspecs.upper ? "NAN" : "nan");
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
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.

EXPECT_EQ("-nan", fmt::format("{}", -nan));
else
EXPECT_EQ(" -nan", fmt::format("{:+06}", -nan));
} else
fmt::print("Warning: compiler doesn't handle negative NaN correctly");
Copy link
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
Copy link
Contributor

vitaut commented May 27, 2021

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