Skip to content

Commit 4a8dbb8

Browse files
committed
Carry intword() to the next unit for values >= 10^24
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 #400.
1 parent 3201e70 commit 4a8dbb8

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎src/humanize/number.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,19 @@ def intword(value: NumberOrString, format: str = "%.1f") -> str:
255255
chopped = value / power
256256
rounded_value = float(format % chopped)
257257

258-
if not largest_ordinal and rounded_value * power == powers[ordinal + 1]:
259-
# After rounding, we end up just at the next power
258+
if not largest_ordinal and rounded_value >= powers[ordinal + 1] // power:
259+
# After rounding, the mantissa reached the next power's boundary
260+
# (e.g. 999_999 formats to "1000.0", which is 1.0 million).
261+
#
262+
# Compare the rounded mantissa against the integer ratio between
263+
# adjacent powers, not `rounded_value * power == powers[ordinal + 1]`:
264+
# that check multiplied a float by a large int, and `float(10**k)`
265+
# is not exactly `10**k` for k >= 24, so the equality was silently
266+
# False from septillion upward and the carry was skipped
267+
# (`10**24 - 1` rendered as "1000.0 sextillion", not "1.0 septillion").
268+
# Every entry in `powers` is a power of ten, so the ratio is exact,
269+
# and it stays large across the decillion-to-googol gap, so a value
270+
# like `10**36` is left as "1000.0 decillion" exactly as before.
260271
ordinal += 1
261272
rounded_value = 1.0
262273

‎tests/test_number.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@ def test_intword_powers() -> None:
116116
(["3500000000000000000000"], "3.5 sextillion"),
117117
(["8100000000000000000000000000000000"], "8.1 decillion"),
118118
(["-8100000000000000000000000000000000"], "-8.1 decillion"),
119+
# A value just under a power boundary must carry to the next unit after
120+
# rounding. The carry check multiplied the rounded mantissa by the
121+
# power and compared to an exact int power; `float(10**k) != 10**k` for
122+
# k >= 24, so from septillion up these silently rendered as
123+
# "1000.0 <smaller unit>" instead of carrying.
124+
([10**24 - 1], "1.0 septillion"),
125+
([10**27 - 1], "1.0 octillion"),
126+
([10**30 - 1], "1.0 nonillion"),
127+
([10**33 - 1], "1.0 decillion"),
128+
# ...but the decillion-to-googol gap has no unit to carry into, so a
129+
# value in it stays a large decillion count (unchanged behaviour).
119130
([1_000_000_000_000_000_000_000_000_000_000_000_000], "1000.0 decillion"),
120131
([1_100_000_000_000_000_000_000_000_000_000_000_000], "1100.0 decillion"),
121132
([2_100_000_000_000_000_000_000_000_000_000_000_000], "2100.0 decillion"),

0 commit comments

Comments
 (0)