fix: ideal_unit_std picks the wrong unit near power-of-unit boundaries#175
Merged
robjtede merged 1 commit intoJul 3, 2026
Conversation
…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.
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 |
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.
Ran into this while comparing
stdvsno_stdoutput for the same byte count:ideal_unit_stdusesf64::ln()to pick the exponent, butln()just isn't precise enough right at a1024^k/1000^kboundary. For example1125899906842623bytes (one byte short of a pebibyte) prints"1.0 PiB"with thestdfeature 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 ofpowichecks 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.