Fix millify() producing out-of-range mantissa like "1000k" - #13
Open
patchwright wants to merge 1 commit into
Open
Fix millify() producing out-of-range mantissa like "1000k"#13patchwright wants to merge 1 commit into
patchwright wants to merge 1 commit into
Conversation
The suffix index is chosen from log10(n) before the mantissa is rounded to `precision`. When the rounded value reaches 1000 (e.g. millify(999999) -> "1000k", or 999500 -> "1000k"), the result is no longer in the [1, 1000) range the suffix represents. Carry the value into the next unit when the rounded mantissa is >= 1000 and a larger suffix is available, so millify(999999) -> "1M". Adds test_millify.py with regression cases (the repo had no tests).
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.
Description
millify()can return a mantissa of1000with the smaller unit suffix,which is outside the
[1, 1000)range each suffix is meant to represent:Root cause
The suffix index
millidxis derived fromlog10(abs(n)) / 3before themantissa is rounded to
precisiondigits. When the rounded value reaches1000(e.g.
999999 / 1000 = 999.999, which rounds to1000), the suffix is still thesmaller one, so the output is
"1000k"instead of carrying into the next unit.Fix
After rounding, if the mantissa is
>= 1000and a larger suffix is available,bump
millidxand recompute. This keeps the mantissa in range and leaves thetop of the suffix table untouched (no overflow past
"Y").Tests
The repository had no test suite, so this adds
test_millify.py(pytest) with:0,999,1000,1M, negative, precision),999999/999500/-999999→1M,999999999→1B), which fail onmasterand pass with the fix,Y) does not overflow.All 12 pass with the fix.