Skip to content

Fix metric() rounding carry stopping at quetta - #450

Open
Vinayak19112003 wants to merge 1 commit into
python-humanize:mainfrom
Vinayak19112003:fix-metric-quetta-carry-423
Open

Vinayak19112003 wants to merge 1 commit into
python-humanize:mainfrom
Vinayak19112003:fix-metric-quetta-carry-423

Conversation

@Vinayak19112003

Copy link
Copy Markdown

Closes #423.

The rounding-carry absorption in metric() was guarded by exponent < 30, so it stopped at quetta (10^30). Values rounding up kept too many digits, and values rounding past quetta were never moved to scientific notation:

>>> metric(9.996e30)
'10.00 Q'   # 4 significant digits, should be 3
>>> metric(9.996e32)
'1000 Q'    # should fall back to scientific

The guard is removed; after the carry, if the exponent reaches 33 (past the largest SI prefix), the function falls back to scientific() as it does for inputs that start there:

>>> metric(9.996e30)
'10.0 Q'
>>> metric(9.996e31)
'100 Q'
>>> metric(9.996e32)
'1.00 x 10\u00b3\u00b3'

Tests: new test_metric_rounding_carry_at_quetta (5 cases) — fails on main, passes with the fix. Full tests/test_number.py: 246 passed. Ruff check and format clean.

@Wkhanex Wkhanex left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I checked the quetta carry boundary in both signs: 9.996e30 → 10.0 Q, 9.996e31 → 100 Q, and 9.996e32 → 1.00 x 10³³, with the corresponding negative outputs. All six passed at this head. Keeping the original value for the scientific fallback avoids formatting the already-rescaled mantissa as if it were the original number. Consider adding negative cases alongside the new positive regressions. I ran these checks on Python 3.12 with English locale hooks, not a full package suite.

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.

metric() adds a digit when a value rounds into quetta

2 participants