Skip to content

fix: bump the unit when precision reaches the ceiling, without rounding first - #414

Open
bardrop wants to merge 1 commit into
avoidwork:masterfrom
bardrop-oss:fix-precision-unit-ceiling
Open

bardrop wants to merge 1 commit into
avoidwork:masterfrom
bardrop-oss:fix-precision-unit-ceiling

Conversation

@bardrop

@bardrop bardrop commented Nov 15, 2026

Copy link
Copy Markdown

Summary

Two fixes in applyPrecisionHandling(), the step that moves a precision result to the next unit:

  • Move up at the ceiling, not only on e-notation. Since fix: apply precision before round for decimal units #404, (999.999).toPrecision(4) is "1000", which has no e, so filesize(999999, { precision: 4 }) printed 1000 kB (and 1048575 with base: 2 printed 1024 KiB). It now prints 1.000 MB / 1.000 MiB, as 11.0.30 did and as filesize(999999) → 1 MB does.
  • Don't round to round before toPrecision() after the move. This is the same double rounding fix: precision is applied after round, rounding an already-rounded value #399/fix: apply precision before round for decimal units #404 removed from the main path. filesize(1024000, { base: 2, precision: 3 }) printed 0.980 MiB (true value 0.9766), and with round: 0 it printed 0: filesize(392, { precision: 1, round: 0 }) gave 0 kB. The new value now comes straight from the input, so roundingFunc and round are dropped from the helper's arguments (call site, helper tests and TECHNICAL_DOCUMENTATION.md updated).

Two existing pins of 0.990 GiB / 0.990 gibibytes for 1058223158 are updated to 0.986: 1058223158 / 2^30 = 0.98555, so three significant digits are 0.986.

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

Fixes #413

Testing

  • Two new tests in Precision option (ceiling bump for SI / IEC / bits / negative / object and exponent output / forced exponent, and digits kept after the bump). They fail on master (1000 kB, 0.980 MiB) and pass with the fix.
  • npm test: lint clean, 281 pass / 0 fail, coverage still 100% for all files.
  • Old-vs-new diff over ~1.9M inputs (random sizes 1 B–1e28 plus every unit boundary, precision 0–5, base 2/10, bits, round 0/2): no output changes when precision is 0. Every change is in the bump path above. Checking the results against the exact value: above whole bytes, no result now reaches the ceiling or is off by more than half a unit in the last significant digit (master had ~198k such results in that run, mostly the round: 0 case).

Checklist

  • npm test passes
  • 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
  • CHANGELOG.md updated (if applicable)

…ng first

applyPrecisionHandling() only moved to the next unit when toPrecision()
returned e-notation. Since avoidwork#404 the value is no longer rounded to `round`
before the precision pass, so 999.999 kB with precision 4 gives "1000"
and printed "1000 kB" (filesize(999999) prints "1 MB"). Move up when the
significant-digit form reaches `ceil` too.

When the unit moved, the recomputed value was still rounded to `round`
decimals before toPrecision(), the same double rounding avoidwork#399 fixed in the
main path: 1000 KiB printed "0.980 MiB" instead of "0.977 MiB", and with
round: 0, 392 B at precision 1 printed "0 kB". Take the new value straight
from the input; roundingFunc and round are no longer needed here.

Fixes avoidwork#413

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.

fix: precision leaves 1000 kB in the old unit and rounds to round before the unit bump

1 participant