Skip to content

fix(site): round figures exactly as the report does, near ties included - #93

Merged
TMHSDigital merged 1 commit into
mainfrom
fix/fixed4-rounding
Sep 24, 2026
Merged

TMHSDigital merged 1 commit into
mainfrom
fix/fixed4-rounding

Conversation

@TMHSDigital

Copy link
Copy Markdown
Owner

Fixes #48.

The bug. fixed4 in site/floor.js is meant to round half to even only on an exact tie, the way Python's :.4f does. Its tie test compared x * 10000 === scaled, and scaled was defined as x * 10000, so the test was always true. Every near tie was forced to half-even. For example, a measured ECE of 0.00125 printed as 0.0012 on the site, while the report prints 0.0013.

The fix. toFixed already rounds the exact binary value. It differs from Python only on an exact tie, where it rounds away from zero and Python rounds to even.

  • A four-place tie is x = (2k + 1) / 20000.
  • A double is a dyadic rational and 20000 = 32 * 625, so a double can sit exactly on a tie only when x * 32 is an odd integer (0.03125, 0.09375, ...).
  • Multiplying by 32 is exact, so the new test finds exact ties and nothing else. Everything that isn't an exact tie goes to toFixed.

Evidence.

values checked against Python before after
51,972 on and beside every tie up to 2.0 (scratch harness) 10,575 differ 0 differ
890 in the new golden fixed4 table 142 differ 0 differ

Coverage. scripts/floor_golden.py now exports a fixed4 table: every exact tie below 2, plus near ties with both float neighbours across the ECE range. It also adds 0.00125 and 0.03125 to the measured values each verdict sentence is exported for. --check compares the table too, and check_floor_parity.mjs fails on any disagreement.

With the old floor.js, the parity check now fails: 142 values are misformatted, and every case's verdict for ECE 0.00125 differs. The fixture grows by about 30 KB.

Checks. ruff, format, mypy --strict and pytest pass. The site build, parity (including the worked example), links, search and the browser smoke test all pass.

🤖 Generated with Claude Code

fixed4's exact-tie test compared x * 10000 with itself, so it was always
true and every near tie was rounded half to even, printing 0.00125 as
0.0012 where Python prints 0.0013. A double sits exactly on a four-place
tie only when x * 32 is an odd integer, and that product is exact, so the
formatter now checks that and otherwise leaves toFixed to round the exact
value. The golden fixture carries 890 tie and near-tie values and two
near-tie measured values, which the old formatter fails and this passes.

Fixes #48.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@TMHSDigital
TMHSDigital merged commit 5c941da into main Sep 24, 2026
23 checks passed
@TMHSDigital
TMHSDigital deleted the fix/fixed4-rounding branch September 24, 2026 22:26
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.

The site's fixed4 rounds differently from Python's %.4f

1 participant