Fix intword() rounding carry for very large numbers#346
Open
gaoflow wants to merge 1 commit into
Open
Conversation
intword() detected the post-rounding carry into the next magnitude with 'rounded_value * power == powers[ordinal + 1]'. Above ~10**22 that product is evaluated in floating point and no longer equals the exact next power, so the carry was skipped and the value was rendered against the lower magnitude: intword(10**24 - 1) returned '1000.0 sextillion' instead of '1.0 septillion' (same for 10**27, 10**30, 10**33). Compare rounded_value against the exact integer ratio powers[ordinal+1] // power instead, keeping the comparison in exact integer/short-float terms. This mirrors the recently fixed carry handling in metric() (python-humanize#328) and naturalsize() (python-humanize#329).
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #346 +/- ##
=======================================
Coverage 99.56% 99.56%
=======================================
Files 12 12
Lines 913 925 +12
=======================================
+ Hits 909 921 +12
Misses 4 4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
intword()decides whether rounding pushed a value up into the next magnitudewith:
For values above ~
10**22,rounded_value * poweris evaluated in floatingpoint and no longer equals the exact
powers[ordinal + 1], so the carry isskipped and the number is rendered against the lower magnitude:
The same happens at
10**30and10**33.Fix
Compare
rounded_valueagainst the exact integer ratiopowers[ordinal + 1] // power, keeping the check in exact integer terms ratherthan relying on a large float product. This mirrors the recently fixed carry
handling in
metric()(#328) andnaturalsize()(#329).Tests
test_intword_rounding_rolloverasserts the correct roll-over across everyaffected magnitude (
10**24/27/30/33 - 1), plus a couple of just-below casesthat must not roll over, guarding both directions.
pytest→ 716 passed.ruff check/ruff format --checkclean.