Skip to content

literal: round-half-even tie for normal-range float/complex repr#8243

Draft
youknowone wants to merge 1 commit into
RustPython:mainfrom
youknowone:float-repr-tie
Draft

literal: round-half-even tie for normal-range float/complex repr#8243
youknowone wants to merge 1 commit into
RustPython:mainfrom
youknowone:float-repr-tie

Conversation

@youknowone

Copy link
Copy Markdown
Member

float::to_string's normal-magnitude branch and complex::component_to_string returned Rust's shortest formatting directly. Rust's formatter can land on the odd-digit neighbour of a rounding tie where repr()'s round-half-to-even picks the even one, e.g. bits 0x42e26687db6b9b04 format as 161852602146008.13 but CPython repr gives 161852602146008.12 (both round-trip).

The scientific branch of float::to_string already ran through prefer_cpython_tie_repr; this extends that helper to fixed-notation strings and applies it to the normal-magnitude float branch and to both complex branches.

Verified against CPython 3.14 over a differential battery (20k floats, 280k complex pairs incl. subnormals / powers of ten / ties): 0 divergences after the fix. Added a normal-range tie unit test.

assisted by Claude

to_string's normal-magnitude branch and complex::component_to_string
returned Rust's shortest formatting directly, which can pick the odd-digit
neighbour of a rounding tie where repr() picks the even one (e.g. bits
0x42e26687db6b9b04 formats as 161852602146008.13 vs repr's ...08.12).
Route both through prefer_cpython_tie_repr, generalized to fixed-notation
strings. Add a normal-range tie unit test.

Assisted-by: Claude
@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: bf700879-19e0-4a36-b33c-e74c94c20537

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

}

#[test]
fn repr_normal_range_uses_cpython_tie_digit() {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Add also extra_tests

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.

1 participant