Fix intword() rounding carry for values >= 10^24 - #401
Closed
Sreekant13 wants to merge 1 commit into
Closed
Sreekant13 wants to merge 1 commit into
Sreekant13 wants to merge 1 commit into
Conversation
The rounding carry compared `rounded_value * power == powers[ordinal + 1]`, a float product against an exact int power. `float(10**k) != 10**k` for k >= 24, so from septillion up the equality was False and the carry was skipped: `10**24 - 1` rendered as "1000.0 sextillion" instead of "1.0 septillion". Compare the rounded mantissa against the exact integer ratio between adjacent powers instead; every power is a power of ten so the ratio is exact, and it stays large across the decillion-to-googol gap, so `10**36` is unchanged at "1000.0 decillion". Fixes python-humanize#400.
Sreekant13
force-pushed
the
fix-intword-carry-at-scale
branch
from
September 16, 2026 03:22
58bd267 to
4a8dbb8
Compare
Author
|
Closing as a duplicate of #346, which fixes this same For what it is worth as an independent confirmation for reviewers: I reproduced the exact same boundary (the carry fails from 10^24 through 10^33, works below because |
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.
Fixes #400.
intword()'s rounding carry (so999_999reads as"1.0 million", not"1000.0 thousand") silently stopped firing from septillion (10^24) upward:Cause
The carry compared a float product against an exact int power:
powersholds Python ints, andfloat(10**k) != 10**koncek >= 24, so1000.0 * 10**21 == 10**24isFalseand the carry was skipped. Below10**24the intermediate is float-exact, so it worked, which is why it went unnoticed.Fix
Compare the rounded mantissa against the integer ratio between adjacent powers:
Every entry in
powersis a power of ten, so the ratio is exact. It stays large (10**67) across the decillion-to-googol gap, so10**36is still"1000.0 decillion"and the #356 behaviour is unchanged.Tests
Added
10**24 - 1...10**33 - 1to thetest_intwordtable (all four fail onmain, showing"1000.0 <smaller unit>"), plus the existing10**36 -> "1000.0 decillion"case kept right after them as the control that the gap is untouched. Fulltest_number.pypasses (236), and the existing small-number carries (999_999 -> "1.0 million", etc.) are unaffected.