Commit 4a75370
committed
Fix naturalsize() rounding rollover at unit boundaries
naturalsize() chooses the suffix from the unrounded byte count via
int(min(log(abs_bytes, base), ...)), then rounds the mantissa with the
format string afterward. When rounding pushes the mantissa up to the base,
the already-chosen suffix is left stale:
>>> naturalsize(999999)
'1000.0 kB' # expected '1.0 MB'
>>> naturalsize(999999999)
'1000.0 MB' # expected '1.0 GB'
>>> naturalsize(1024 ** 2 - 1, binary=True)
'1024.0 KiB' # expected '1.0 MiB'
This is distinct from the ZB->YB top-boundary fix in #206 (which added
larger suffixes but did not touch the rounding) and from the metric() fix
in #328 (a different function). Here the rollover happens at every small
unit too.
Fix: after rounding, if the mantissa has reached the base and a larger
suffix is available, step up one suffix. Added regression cases to
test_naturalsize (they fail before this change, pass after); all existing
assertions and documented examples are unchanged.1 parent 976484a commit 4a75370
2 files changed
Lines changed: 15 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
100 | 106 | | |
101 | 107 | | |
102 | 108 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
85 | 94 | | |
86 | 95 | | |
87 | 96 | | |
| |||
0 commit comments