Fix percent rounding: 2.55 is not exactly representable (rgb(50%, 50%, 50%) → 127, expected 128)#92
Open
lanestp wants to merge 1 commit into
Open
Fix percent rounding: 2.55 is not exactly representable (rgb(50%, 50%, 50%) → 127, expected 128)#92lanestp wants to merge 1 commit into
lanestp wants to merge 1 commit into
Conversation
rgb() percentage channels convert via Math.round(p * 2.55). 2.55 has no exact float64 representation (2.5499999999999998…), so the product lands just below the half-integer for p = 50 and p = 90: rgb(50%, 50%, 50%) parses to [127, 127, 127] instead of [128, 128, 128], and 90% gives 229 instead of 230. Math.round(p * 255 / 100) computes the conversion exactly. Sweeping all integer percents 0-100 (and all tenths/hundredths, verified against exact rational arithmetic): 50 and 90 are the only values that change. The suite pinned the old values in nine assertions (updated) and two regression cases are added. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
rgb()percentage channels convert viaMath.round(p * 2.55). Since2.55has no exact float64 representation (it's2.5499999999999998…), the product lands just below the half-integer forp = 50andp = 90:The fix:
Math.round(p * 255 / 100), which computes the conversion exactly and is self-documenting. If you'd rather keep the single multiply,Math.round(p * 2.5500000000000003)(the next representable double above 2.55) also works — both candidates were verified against exact rational arithmetic over ~11k percent inputs (all integers, tenths, hundredths, plus adversarial near-half-integer decimals), zero divergences for either. Happy to switch to whichever you prefer.Blast radius, measured exhaustively: across integer percents 0–100, exactly two values change —
50% : 127 → 128and90% : 229 → 230. Everything else is byte-identical.Tests: the existing suite pinned the old values in nine assertions (
[255, 77, 229, …]for the30%/90%cases) — updated here — and two regression cases are added with a short comment on the float mechanics.Found via characterization testing while building verification tooling against color-string. It's a (tiny) behavioral change, so version it however you see fit — happy to adjust anything.