Skip to content

fix: keep exact BigInt values exact with floor and ceil rounding - #431

Open
bardrop wants to merge 1 commit into
avoidwork:masterfrom
bardrop-oss:fix-bigint-whole-value
Open

bardrop wants to merge 1 commit into
avoidwork:masterfrom
bardrop-oss:fix-bigint-whole-value

Conversation

@bardrop

@bardrop bardrop commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

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.

filesize(59033n, { exponent: 0, roundingMethod: "floor" }); // "59032 B"     (want "59033 B")
filesize(59031n, { exponent: 0, roundingMethod: "ceil" });  // "59032 B"     (want "59031 B")
filesize(76745800n, { exponent: 1, roundingMethod: "floor" }); // "76745.79 kB" (want "76745.8 kB")
filesize(246770688n, { base: 2, exponent: 1, roundingMethod: "floor" }); // "240986.99 KiB" (want "240987 KiB")

The number input gives the right answer for all four.

How often: with exponent: 0 and floor, 3252 of the BigInts 1..200000 are wrong (3254 with ceil). With exponent 1-3 and values that are exact in that unit, about 1% are wrong.

Cause

calculateBigIntValue() does Number((num * 10n ** 16n) / power) / 1e16. That rounds two times: once in Number(...) (the scaled value is above 2^53) and once in the division. So an exact result like 59033 becomes 59032.99999999999, and Math.floor turns it into 59032.

Fix

When num and power both 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 above Number.MAX_SAFE_INTEGER, or 10^24) keep the scaled division.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature / enhancement
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Build / dependency / tooling change
  • Test additions or updates
  • Other

Related Issues

N/A (no issue filed). The BigInt path came from #354 / #355.

Testing

  • New test in 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.
  • I compared filesize(n, opts) with filesize(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 test passes (286 tests)
  • npm run build succeeds
  • 100% test coverage maintained
  • No hardcoded secrets or credentials introduced
  • Zero external dependencies added
  • ES Modules only (no CommonJS in src/)
  • JSDoc comments added/updated: N/A (no signature change)
  • CHANGELOG.md updated (if applicable): N/A (generated at release)

Screenshots (if applicable)

N/A

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant