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
|
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' |
|
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. |
|
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.
|
Good catch. Those weren't covered: |
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.