Repository navigation
Conversation
This branch has not been deployed
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.
Summary
A BigInt input can come out one step too low with
roundingMethod: "floor", or one step too high with"ceil". The same value as a number is fine.The number input gives the right answer for all four.
How often: with
exponent: 0andfloor, 3252 of the BigInts 1..200000 are wrong (3254 withceil). Withexponent1-3 and values that are exact in that unit, about 1% are wrong.Cause
calculateBigIntValue()doesNumber((num * 10n ** 16n) / power) / 1e16. That rounds two times: once inNumber(...)(the scaled value is above 2^53) and once in the division. So an exact result like 59033 becomes 59032.99999999999, andMath.floorturns it into 59032.Fix
When
numandpowerboth fit in a number with no loss, divide them as numbers. One division gives the correctly rounded value, the same as the number path. Other values (for example aboveNumber.MAX_SAFE_INTEGER, or 10^24) keep the scaled division.Type of Change
Related Issues
N/A (no issue filed). The BigInt path came from #354 / #355.
Testing
tests/unit/filesize.test.js("should keep an exact whole value with floor and ceil rounding"). It fails before the fix ('59032 B'vs'59033 B') and passes after.filesize(n, opts)withfilesize(BigInt(n), opts)for 400,000 random safe integers over 2880 option sets (base, bits, round, precision, roundingMethod, exponent, standard), object output. Before the fix this finds mismatches with floor and ceil; after it, 0.Checklist
npm testpasses (286 tests)npm run buildsucceedsScreenshots (if applicable)
N/A