Skip to content

feat: add ROUND_HALF_TO_EVEN flag for banker's rounding (fixes #251) - #319

Closed
jdymitarai wants to merge 1 commit into
google:masterfrom
jdymitarai:feat-round-half-to-even-251
Closed

jdymitarai wants to merge 1 commit into
google:masterfrom
jdymitarai:feat-round-half-to-even-251

Conversation

@jdymitarai

Copy link
Copy Markdown
Contributor

Summary

Addresses feature request #251.

By default, \DoubleToStringConverter\ rounds halfway cases away from zero (towards +/-Infinity). However, modern C++ formatting standards (\std::to_chars, \printf, and IEEE 754 default rounding mode) use round-half-to-even (banker's rounding).

This PR introduces an opt-in flag \ROUND_HALF_TO_EVEN = 128\ to \DoubleToStringConverter::Flags\ without altering existing default behavior.

Key Changes

  • Flag Definition: Added \DoubleToStringConverter::Flags::ROUND_HALF_TO_EVEN = 128\ with documentation.
  • Propagation: Passed the flag down through \DoubleToAscii\ and \BignumDtoa.
  • Rounding Logic: In \GenerateCountedDigits\ and \BignumToFixed, when the remainder is exact halfway (\PlusCompare == 0), the digit increments only if odd when
    ound_half_to_even\ is enabled.
  • Zero Regression / 100% Backward Compatibility: Default conversions without this flag continue using the exact same fast-paths (\FastDtoa, \FastFixedDtoa) and existing rounding rules.
  • Unit Tests: Added \TEST(RoundHalfToEven)\ in \ est/cctest/test-conversions.cc\ testing exact dyadic fractions (e.g. 0.5, 1.5, 2.5, 3.5, 4.5, -0.5, -1.5, 0.25, 0.75, 1.25, 1.75, 2.25, 0.125, 0.375, 0.625, 0.875) across \ToFixed, \ToPrecision, and \DoubleToAscii. All 9 test suites pass cleanly.

Closes #251.

@jdymitarai

Copy link
Copy Markdown
Contributor Author

Closing for now to keep the PR queue focused. Thanks!

@jdymitarai jdymitarai closed this Sep 12, 2026
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.

Feature request: rounding half to even

1 participant