Repository navigation
Give integer / and % Java's semantics for negative operands - #566
CatarinaGamboa wants to merge 3 commits into
Conversation
Z3's integer div and mod are Euclidean: the remainder is never negative. Java rounds the quotient toward zero and gives the remainder the sign of the dividend, so -7 / 2 was -4 and -7 % 2 was 1 in the solver. Build Java's result from Z3's, which only differs when the dividend is negative and the division is not exact. Longs, which are reals in the solver, now use the same integer division instead of real division. Fixes #564 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Longs are reals in the solver, which may pick non-integral values for them. Converting the operands to integers first could turn a positive divisor into zero and lose the quotient's sign, so x / y with x < 0 and y > 0 was no longer known to be at most zero. Move the parity check to a fixture no other test counts errors in. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex review, round 1PR regression — valid long division contract is rejected. @Refinement("_ <= 0")
static long divide(@Refinement("_ < 0") long x,
@Refinement("_ > 0") long y) {
return x / y;
}Java always returns a nonpositive value here. The PR reports a refinement error with Pre-existing soundness bug — overflow is accepted as positive. @Refinement("_ > 0")
static int badInt() { return Integer.MIN_VALUE / -1; }
@Refinement("_ > 0")
static long badLong() { return Long.MIN_VALUE / -1L; }Java returns the negative minimum value in both cases, so both contracts should be rejected. Both the PR and I also exercised negative operands, symbolic int and long parameters, mixed int/long operations, Response
|
Codex review, round 2PR-caused regression: valid @Refinement("_ == x")
static long divide(long x) { return x / 1L; }
@Refinement("_ == 0")
static long remainder(long x) { return x % 1L; }Both must pass for every Java I found no false acceptance in the other tested cases, covering negative operands, int/long mixes, refined parameters, contracts, ResponseNot changed in this PR; the cause is that longs are unconstrained reals, which no rounding encoding can fully hide. I compared the options on 9 valid and 7 invalid long programs (each method verified on its own):
The PR keeps the rounded real quotient: sound, and as complete as main on these programs. Modelling |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Clean-code reviewAudit of the diff (clean-code-principles):
Re-checked after the changes: test suite green, the Z3 checks of the encoding still pass, and the error-message study tasks give identical verdicts and messages. |
Fixes #564
Z3's integer
divandmodare Euclidean: the remainder is never negative. Java's/rounds toward zero and%has the sign of the dividend, so the solver thought-7 / 2 == -4and-7 % 2 == 1, and wrong programs passed, e.g. a parity checkx % 2refined as_ == 0 || _ == 1.Change
TranslatorToZ3.makeDiv/makeMod(used both for refinements and for code) now build Java's result from Z3's. The two only differ when the dividend is negative and the division is not exact:Division by zero is unchanged:
a div 0stays unconstrained, soa / 0anda % 0are still any value.longvalues are reals in the solver, and/on them was real division (7L / 2L == 3.5, so@Refinement("_ * 2 == 7") long q = 7L / 2L;passed). Long/is now the real quotient rounded toward zero, anda % bisa - b * (a / b). This is Java's result for the integral values longs take, and keeps the quotient's sign when the solver picks non-integral values for longs (converting the operands to integers first turned a divisor of1/2into 0).Constant folding (
VCFolding,StaticConstants.foldIntegral) already used Java's operators, so messages showed Java's value even when the solver disagreed (on main,@Refinement("_ == -3") int n = -7 / 2;reported "foundn == -3but expectedn == -3").How it was checked
aandb != 0, that the encodedq,rsatisfya == b*q + r,|r| < |b|,ris 0 or has the sign ofa, and|b*q| <= |a|, which define Java's results. The same property is satisfiable (fails) for Z3's plaindiv/mod, so the check is not vacuous.makeDiv/makeModon literals match Java's/and%for all|a|, |b| <= 20,b != 0, for int and long (6560 checks)./=and%=;_ == a / band_ == a % bcontracts called with negative arguments;x % 2 == 1parity on negativex;x % y >= 0withy > 0; longs. Wrong ones are now rejected with counterexamples such asx == -1 && #ret == -1; right ones (like_ % 2 == 1implies_ > 0, orx > -2impliesx / 2 >= 0) now pass.Limitation: long is still a real
The solver can still pick non-integral values for long variables, so a few valid long programs are rejected, e.g.
x / 1L == x,x % 1L == 0andx * 2 / 2 == x(main accepted these, but it rejected others, such asx > -2impliesx / 2 >= 0, and accepted wrong ones). See the comparison in the Codex round 2 comment. Modelling long as an Int is the full fix and is left for a separate issue.Tests
Extended
CorrectArithmeticBinaryOperations(negative/,%and long/) andErrorSpecificArithmetic(parity). Both fail on main.🤖 Generated with Claude Code