Fix format_size() rounding rollover at unit boundaries#82
Open
patchwright wants to merge 1 commit into
Open
Conversation
format_size() chooses the unit by comparing the raw byte count to each
divider, then rounds the mantissa with round_number() afterward. When the
mantissa rounds up to the base (e.g. 999999 bytes is 999.999 KB, which rounds
to '1000 KB'), the already-chosen unit is left stale:
>>> format_size(999999)
'1000 KB' # expected '1 MB'
>>> format_size(999999999)
'1000 MB' # expected '1 GB'
>>> format_size(1024 ** 2 - 1, binary=True)
'1024 KiB' # expected '1 MiB'
Fix: after rounding, if the mantissa has reached the base and a larger unit
is available, carry into that next unit and re-render. Added regression cases
to test_format_size (they fail before this change, pass after); all existing
assertions are unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
format_size()picks the unit by comparing the raw byte count to each divider, then rounds the mantissa withround_number()afterward. When rounding pushes the mantissa up to the base, the already-chosen unit is left stale:999999bytes is999.999 KB;'%.2f'rounds that to1000, but the unit was already fixed atKB, so the output reads1000 KBinstead of1 MB. The same happens at every decimal and binary boundary.Fix
After rounding, if the mantissa has reached the base (
1000decimal /1024binary) and a larger unit is available, carry into that next unit and re-render. Values already at the largest unit (YB/YiB) are unaffected, as are all values that don't round across a boundary.Tests
Added regression assertions to
test_format_sizecovering the decimal and binary boundaries. They fail on the current code and pass with this change; every existing assertion is unchanged.