Skip to content

Reject rounded sums of wide binary strings - #874

Open
yansawu wants to merge 6 commits into
sysprog21:mainfrom
yansawu:fix/add-binary-wide-cases
Open

yansawu wants to merge 6 commits into
sysprog21:mainfrom
yansawu:fix/add-binary-wide-cases

Conversation

@yansawu

@yansawu yansawu commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem and behavior

The five original Wide Register Arithmetic (add-binary) cases use binary strings of at most four bits. A JavaScript solution that adds parseInt(a, 2) and parseInt(b, 2) therefore passes 5/5, even though it rounds 2^53 + 1 to 2^53. This input is valid under the problem's 10,000-character constraint.

Approach

Append five cases to cover precision loss, fixed-width truncation and the maximum input length:

  • wide value plus one: 2^53 + 1, the case accepted in the issue, rejects addition through Number.
  • wide odd value plus zero: preserves 2^53 + 1 unchanged, rejecting a solution that converts an already rounded Number to BigInt.
  • wide all ones plus one: 128 ones plus one requires a 129-bit result, rejecting both 64-bit and unsigned 128-bit truncation. The modeled wrong implementation uses 64 bits; a comment explains the stronger case.
  • two wide values with multi-bit carry: unequal 64-bit and 56-bit addends require a 65-bit exact result, rejecting conversion of only the shorter operand through Number.
  • maximum length all ones plus one: 10,000 ones plus one requires a 10,001-bit result, rejecting the reported Python solution that recurses for each character and exceeds the default recursion limit.

The arithmetic regression in tests/browser/compiler-explorer.test.js runs five wrong implementations by name and looks up the case each must fail by label. The reported parseInt solution is the only Number-based addition variant; the equivalent binary-literal conversion is omitted. A separate regression rejects preserving only the longer operand exactly, which passes all eight earlier cases, and confirms that the new addends both exceed 53 bits and differ in length. The same wide case must also reject an adder that writes 0 when a column sums to 3, so later fixture edits cannot silently remove that coverage. Bit-by-bit carry and direct BigInt addition match every expected value.

Repeatable tests also run correct solutions through the generated C, C++ and Java harnesses in tests/browser/compiler-explorer.test.js and the Python driver in tests/browser/python-driver.test.js. Each test reads the served judge, requires a result wider than 64 bits, checks that every case ran without error, and compares every returned string with its expected value.

A named regression in tests/browser/python-driver.test.js requires the recursive candidate to pass the first nine cases and return RecursionError on the maximum-length case, looked up by label. The correct Python candidate now uses iterative per-digit carry, following the problem's clarification against converting whole operands to integers.

The cases are appended so existing example indexes remain unchanged. Their labels join the contract checks in tests/web/contract.rs; web/judges/wide-register-arithmetic.json is regenerated, and the add-binary digest in tests/golden/task-posed.json is updated.

Validation

  • OpenJDK and javac 21.0.13.1 installed and used locally.
  • Focused add-binary regressions and language-driver tests: 11 passed, 0 skipped. Each language's correct solution passes all ten judge cases; the reported Number solution passes 6/10.
  • Before adding the maximum-length case, the new recursion regression fails on its missing label. With the case, the recursive candidate passes 9/10 and returns RecursionError on the pinned maximum-length input.
  • ./scripts/test.sh: passed locally (exit 0). Rust: 3,066 passed, 29 ignored. Browser Node suite: 2,349 passed, 0 failed, 1,443 skipped. Git hooks: 498 checks passed.

Local coverage limits: the gate skipped three server/browser checks, Chromium/Playwright and ffmpeg-dependent tests, desktop clippy/tests (missing WebKitGTK development package), one Python authoring probe outside a virtual environment, cargo-audit, cargo-deny and actionlint. The Python driver used CPython; real-browser/Pyodide behavior and live external compiler services were not tested locally. These local results do not establish remote CI success.

Close #848


Summary by cubic

Adds add-binary cases that reject solutions relying on parseInt/Number precision by requiring exact sums of binary strings wider than 2^53. Also checks exact sums through the C, C++, Java, and Python harnesses.

  • Covers 2^53 + 1, an already-roundable odd value, 128 ones plus one (129-bit result), and two wide addends with a multi-bit carry so rounding only the shorter operand is rejected.
  • Adds a maximum-length all-ones plus one case that rejects per-character Python recursion exceeding its stack limit on valid 10,000-character inputs.
  • Shares a carry-loop helper between the correct reference and the column-of-three wrong adder; both stay pinned to the multi-bit carry case so fixture edits can't remove coverage.
  • Appends cases so existing indexes stay stable and updates the browser regression, contract checks, judge data, and the add-binary golden digest.
  • Verifies exact wide sums through C, C++, Java, and Python harnesses, not just string arithmetic in the browser regression.

Closes #848.

Written for commit 4ea6e9d. Summary will update on new commits.

View guided diff

Small binary inputs let Number-based solutions pass despite losing
precision on valid wider values. Require exact sums beyond machine
integer widths.

Close sysprog21#848

Change-Id: I44316c4af61fdbfe0d5b6a1d734e8a42758f345d
cubic-dev-ai[bot]

This comment was marked as resolved.

@jserv

jserv commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Java was not run because this host has no JDK.

You MUST install Java development tools and validate carefully. You are learning to develop software professionally by taking responsibility for the code you submit and making it work for the people who use it. AI tools can help, but the work is yours.

jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

String arithmetic checks leave argument and result handling untested
across language runners. Require exact wide sums through each harness
and keep one regression for the reported Number precision loss.

Close sysprog21#848

Change-Id: I6c33502a39773ef703c04e995357f006596ee65d
ArthurArthurArthur0817

This comment was marked as resolved.

The wide cases pair each long value with a single-bit operand, so
converting only the shorter operand through Number still passes them.
Require two wide addends to expose that precision loss and retain
coverage of carry propagation across unequal lengths.

Close sysprog21#848

Change-Id: I473640088c7758bb29f27b998b059a35e74dc13e

@winter77777 winter77777 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I checked out this branch and ran that solution and a correct one through the C harness (judge builds it with generateHarness("c", ...) from web/compiler-explorer.js, compiles it with gcc and compares each result with expected), first on the eight cases, then with ["11", "11"] added:

$ git log -1 --format='%h %s'
f0ea0702 Reject rounded sums of wide binary strings
$ gcc --version | head -1; node --version
gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0
v24.22.0
$ node --test --test-name-pattern="add-binary" tests/browser/compiler-explorer.test.js
✔ add-binary cases accept exact addition of wide values (1.321748ms)
✔ add-binary cases reject parseInt addition through Number (0.20085ms)
✔ add-binary cases reject binary literals converted to Number (0.113184ms)
✔ add-binary cases reject BigInt converted from an already rounded Number (0.079999ms)
✔ add-binary cases reject addition limited to 64 bits (0.067145ms)
ℹ tests 5
ℹ pass 5
ℹ fail 0
ℹ skipped 0
$ /usr/bin/python3 -c 'import json; j=json.load(open("web/judges/wide-register-arithmetic.json")); j["cases"].append({"label":"two ones with a carry in","input":["11","11"],"expected":"110"}); json.dump(j,open("/tmp/judge874.json","w"))'
$ judge web/judges/wide-register-arithmetic.json $E/sum3.c
$ judge web/judges/wide-register-arithmetic.json $E/correct.c
$ judge /tmp/judge874.json $E/sum3.c
$ judge /tmp/judge874.json $E/correct.c
8 / 8 passed
8 / 8 passed
8 / 9 passed
FAIL two ones with a carry in ['11', '11'] got '100' 
9 / 9 passed

Comment thread problem-bank/judges.json Resolved
jserv

This comment was marked as resolved.

@yansawu

yansawu commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Java was not run because this host has no JDK.

You MUST install Java development tools and validate carefully. You are learning to develop software professionally by taking responsibility for the code you submit and making it work for the people who use it. AI tools can help, but the work is yours.

This is my mistake! Validated locally with OpenJDK 21.0.13.1 (java and javac). At 31da0069, the committed Java harness test compiles and runs all nine add-binary cases, compares every returned string with its expected value, and passes without skips.

The wide case is the only fixture that rejects writing zero when both
bits and an incoming carry sum to three. Pin the wrong adder by name so
later fixture edits cannot silently remove that coverage.

Close sysprog21#848

Change-Id: Iaf81f76c00209152f896ee705f7eea9229fde1e9

@ye144440154 ye144440154 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

At 31da0069, a Python solution that recurses the wrong way passes all the current cases, but a case longer than 1,000 bits triggers RecursionError. I also found that the Python reference solution differs from the other languages and does not follow the problem's clarification.

The PR's own add-binary tests pass on this commit:

$ node --test --test-name-pattern='add-binary' tests/browser/compiler-explorer.test.js tests/browser/python-driver.test.js
ℹ tests 9
ℹ pass 9
ℹ fail 0

Comment thread problem-bank/judges.json Resolved
Comment thread tests/browser/python-driver.test.js Resolved
Per-character Python recursion passes the short wide cases but exceeds
its stack limit on valid long strings. Add a maximum-length carry case
and require the recursive candidate to fail it through the Python
driver. Use iterative carry for the Python reference to follow the
clarification against converting whole operands to integers.

Close sysprog21#848

Change-Id: I88662046d24d1a901211784a070da7cb03079c4e
@github-actions github-actions Bot added the ready for maintainer review The pull request ready for maintainer review. label Nov 26, 2026
jserv

This comment was marked as resolved.

jserv

This comment was marked as resolved.

Duplicated carry loops can drift between the correct reference and the
column-of-three regression. Share their traversal and vary only the
digit calculation so the wrong model isolates the intended arithmetic
error.

Keep the shorter-operand model pinned to the case it must reject.
Requiring it to pass unrelated fixtures would obstruct future fixture
improvements.

Close sysprog21#848

Change-Id: Ied7ffdb24746e4fd918ea9fa958166cfda79fc69

@jserv jserv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Rebase the current branch onto the upstream default branch and rework the series into functionally minimal commits, folding similar ones and enforcing the project's commit message rules.

Comment on lines +431 to +437
[
"addition limited to 64 bits",
"wide all ones plus one",
// The 129-bit case also rejects unsigned 128-bit truncation.
(a, b) =>
BigInt.asUintN(64, BigInt(`0b${a}`) + BigInt(`0b${b}`)).toString(2),
],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The comment says the 129-bit case also rejects unsigned 128-bit truncation, but nothing checks it, so a later edit that shortens wide all ones plus one to 128 ones would drop that coverage without failing a test. A second row turns the claim into a pin and the comment can go:

Suggested change
[
"addition limited to 64 bits",
"wide all ones plus one",
// The 129-bit case also rejects unsigned 128-bit truncation.
(a, b) =>
BigInt.asUintN(64, BigInt(`0b${a}`) + BigInt(`0b${b}`)).toString(2),
],
[
"addition limited to 64 bits",
"wide all ones plus one",
(a, b) =>
BigInt.asUintN(64, BigInt(`0b${a}`) + BigInt(`0b${b}`)).toString(2),
],
[
"addition limited to 128 bits",
"wide all ones plus one",
(a, b) =>
BigInt.asUintN(128, BigInt(`0b${a}`) + BigInt(`0b${b}`)).toString(2),
],

assert.equal(reply.results.length, spec.cases.length);
spec.cases.forEach(({ label: caseLabel, expected }, index) => {
if (caseLabel === label) {
assert.match(reply.results[index].error, /RecursionError/);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If this case ever stops raising (the driver raises sys.setrecursionlimit, or the interpreter default changes), error is undefined and assert.match fails with an argument-type error that names neither the case nor the expected RecursionError. Default it and pass the label:

Suggested change
assert.match(reply.results[index].error, /RecursionError/);
assert.match(
reply.results[index].error ?? "",
/RecursionError/,
caseLabel,
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: problem bank ready for maintainer review The pull request ready for maintainer review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wide Register Arithmetic judge accepts solutions that lose precision on long binary strings

6 participants