Repository navigation
Conversation
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
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. |
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
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
left a comment
There was a problem hiding this comment.
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
This is my mistake! Validated locally with OpenJDK 21.0.13.1 ( |
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
left a comment
There was a problem hiding this comment.
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
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
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
left a comment
There was a problem hiding this comment.
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.
| [ | ||
| "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), | ||
| ], |
There was a problem hiding this comment.
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:
| [ | |
| "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/); |
There was a problem hiding this comment.
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:
| assert.match(reply.results[index].error, /RecursionError/); | |
| assert.match( | |
| reply.results[index].error ?? "", | |
| /RecursionError/, | |
| caseLabel, | |
| ); |
Problem and behavior
The five original Wide Register Arithmetic (
add-binary) cases use binary strings of at most four bits. A JavaScript solution that addsparseInt(a, 2)andparseInt(b, 2)therefore passes 5/5, even though it rounds2^53 + 1to2^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 throughNumber.wide odd value plus zero: preserves2^53 + 1unchanged, rejecting a solution that converts an already roundedNumbertoBigInt.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.jsruns five wrong implementations by name and looks up the case each must fail by label. The reportedparseIntsolution 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 writes0when a column sums to3, so later fixture edits cannot silently remove that coverage. Bit-by-bit carry and directBigIntaddition match every expected value.Repeatable tests also run correct solutions through the generated C, C++ and Java harnesses in
tests/browser/compiler-explorer.test.jsand the Python driver intests/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.jsrequires the recursive candidate to pass the first nine cases and returnRecursionErroron 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.jsonis regenerated, and theadd-binarydigest intests/golden/task-posed.jsonis updated.Validation
javac21.0.13.1 installed and used locally.RecursionErroron 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-binarycases that reject solutions relying onparseInt/Numberprecision by requiring exact sums of binary strings wider than2^53. Also checks exact sums through the C, C++, Java, and Python harnesses.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.add-binarygolden digest.Closes #848.
Written for commit 4ea6e9d. Summary will update on new commits.