Skip to content

fix: ideal_unit_std picks the wrong unit near power-of-unit boundaries#175

Merged
robjtede merged 1 commit into
bytesize-rs:masterfrom
chatman-media:fix/std-display-ln-precision-boundary
Jul 3, 2026
Merged

fix: ideal_unit_std picks the wrong unit near power-of-unit boundaries#175
robjtede merged 1 commit into
bytesize-rs:masterfrom
chatman-media:fix/std-display-ln-precision-boundary

Conversation

@chatman-media

Copy link
Copy Markdown
Contributor

Ran into this while comparing std vs no_std output for the same byte count: ideal_unit_std uses f64::ln() to pick the exponent, but ln() just isn't precise enough right at a 1024^k/1000^k boundary. For example 1125899906842623 bytes (one byte short of a pebibyte) prints "1.0 PiB" with the std feature on but "1024.0 TiB" with it off — same number, different unit, depending on a feature flag. It's the same root cause behind #142's panic, just a case where the exponent comes out one too high instead of tripping the assert.

Fix nudges the ln()-based guess to the exact boundary with a couple of powi checks instead of trusting the log division outright, so it always agrees with the (slower, exact) loop the no_std path uses. Added a couple of tests pinned right at that boundary — they fail on master and pass with the fix.

…nit boundaries

f64::ln() isn't precise enough to trust right at a 1024^k or 1000^k boundary, so
the std Display path could pick a different unit exponent than the no_std loop
for the exact same byte count (e.g. 1125899906842623 bytes prints as 1.0 PiB
with std but 1024.0 TiB without it), and in the worst case the assert on the
ln()-derived exponent could fail and panic (see bytesize-rs#142). Nudges the ln()
approximation to the exact boundary with a couple of integer-exact powi checks
instead of trusting it outright. Added tests covering the boundary directly.

@robjtede robjtede left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice fix, thanks

@robjtede robjtede merged commit 520a557 into bytesize-rs:master Jul 3, 2026
17 of 18 checks passed
@robjtede

robjtede commented Jul 3, 2026

Copy link
Copy Markdown
Member

separately i'd like to do some benchmarking to see if there's any perf regression from this in case there's no reason for 2 impls any more

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.

2 participants