Uh oh!
There was an error while loading. Please reload this page.
fix: carry filesize suffix when mantissa rounds up to base - #600
fix: carry filesize suffix when mantissa rounds up to base#600patchwright wants to merge 3 commits into
Conversation
lurch
commented
Jun 18, 2026
Ahhh, the fun of floating-point rounding 😆 I wonder if it might also be worth adding some unit-tests for the boundary-values? E.g. >>>traditional(1996)
'1.9 KB'>>>traditional(1997)
'2.0 KB'>>>decimal(1950)
'1.9 kB'>>>decimal(1951)
'2.0 kB'>>>binary(1996)
'1.9 KiB'>>>binary(1997)
'2.0 KiB' |
patchwright
commented
Jun 19, 2026
Good call, those within-unit boundary cases are worth pinning down separately from the rollover ones. Added a traditional(1996) =="1.9 KB"traditional(1997) =="2.0 KB"decimal(1950) =="1.9 kB"decimal(1951) =="2.0 kB"binary(1996) =="1.9 KiB"binary(1997) =="2.0 KiB"All pass on the branch. Thanks for the review. |
lurch
commented
Jun 19, 2026
Is it also worth checking the boundaries at e.g. >>>decimal(999_949)
'999.9 kB'>>>decimal(999_950)
'1.0 MB'or do you think those are sufficiently covered by the existing tests? |
Per review on PyFilesystem#600: the rollover tests only pinned the far end (999_999 / 1024**2 - 1), not the first value whose mantissa rounds up to base. Add the carry boundary for all three formatters so the fix's behavior at the rollover point is locked down.
patchwright
commented
Jun 24, 2026
Good catch. Those weren't covered: |
patchwright
commented
Jun 26, 2026
FYI on the red checks: the failing jobs (test (2.7), (3.5) through (3.10), (pypy-2.7/3.6/3.7)) all fail identically on master @77a8562, so they are pre-existing failures on the EOL-Python jobs rather than anything from this diff. As a small upside, |
patchwright
commented
Jul 7, 2026
Quick correction to my last CI note: I called those red |
patchwright
commented
Aug 6, 2026
Checking back in — it's been about a month. The content side (filesize suffix + the boundary tests you asked for) has been stable since 07-07; the only red is the pre-existing |
Problem
All three public functions (
traditional,binary,decimal) can produce impossible output like"1,024.0 KB"instead of"1.0 MB"for values just below a unit boundary.Root cause:
_to_strpicks the suffix from the unrounded value, then formats the mantissa — which can round up tobase, producing an impossible result.Fix
Convert
suffixesto a list before the loop. After computing the mantissa, round and compare againstbase. If the rounded value hitsbase, step up one suffix and recompute:Math: when the current unit is
base**i, dividingsizeby that sameunitgives the value in the next suffix (whose denominator isbase**(i+1), which divides intobase * size / base**(i+1) = size / base**i = size / unit).Also removes the existing TODO comment that flagged this exact issue.
Tests
Added
test_rollover_decimal,test_rollover_traditional, andtest_rollover_binarycovering the unit boundaries for all three functions.