FA-17306 / Floating-point arithmetic / Open access
Nearest floating remainder starts from floor-modulo residue · case 01
Nearest floating remainder starts from floor-modulo residue.
ROOT CAUSE
Nearest floating remainder starts from floor-modulo residue. The faulty expression is r=x%ay.
VERIFIED REPAIR
Apply the contract at this fault site using r=math.fmod(x,ay).
Unsuccessful approach: The attempted local correction r=abs(math.fmod(x,ay)) still violates the explicit regression fixtures.
Case contract
For finite x and finite nonzero y, return the remainder x-n*y with n the nearest integer quotient, ties to even. The output has magnitude at most abs(y)/2; exact zero has the sign of x. Finite results are rendered to eleven significant decimal digits; modeled domain violations and arithmetic errors are explicit strings.
Why this case matters
An offline floating representation model isolates a reproducible arithmetic fault.
1 / The failure
Exit 1"""Failure Map reference implementation. Python standard library only."""
import json
import math
import struct
def render(x):
if math.isnan(x): return 'nan'
if math.isinf(x): return '-infinity' if x<0 else '+infinity'
return format(x,'.11g')
N = 1
observations = []
def solve(x,y):
try:
if y==0: return 'domain'
ay=abs(y)
r=x%ay
a=abs(r)
other=ay-a
odd=math.fmod(abs(x),2*ay)>=ay
if a>other or (a==other and odd):
r-=math.copysign(ay,x)
if r==0: r=math.copysign(0.0,x)
return render(r)
except (ValueError, OverflowError, ZeroDivisionError, TypeError):
return "arithmetic-error"
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('even tie', solve(2.0*N*4+2,4.0), "2")
check('odd tie', solve((2.0*N+1)*4+2,4.0), "-2")
check('negative even tie', solve(-2.0*N*4-2,4.0), "-2")
check('negative odd tie', solve(-(2.0*N+1)*4-2,4.0), "2")
check('above half', solve(4.0*N+3,4.0), "-1")
check('below half', solve(4.0*N+1,4.0), "1")
check('negative divisor below half', solve(4.0*N+1,-4.0), "1")
check('negative divisor', solve(4.0*N+3,-4.0), "-1")
check('negative exact', solve(-4.0*N,4.0), "-0")
check('positive exact', solve(4.0*N,4.0), "0")
check('zero divisor', solve(float(N),0.0), "domain")
check('huge finite', solve(1.5e308,1e308), render(math.remainder(1.5e308,1e308)))
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| even tie | 2 | 2 | Passed |
| odd tie | -2 | -2 | Passed |
| negative even tie | 2 | -2 | Failed |
| negative odd tie | 6 | 2 | Failed |
| above half | -1 | -1 | Passed |
| below half | 1 | 1 | Passed |
| negative divisor below half | 1 | 1 | Passed |
| negative divisor | -1 | -1 | Passed |
| negative exact | -0 | -0 | Passed |
| positive exact | 0 | 0 | Passed |
| zero divisor | domain | domain | Passed |
| huge finite | -5e+307 | -5e+307 | Passed |
SHA-256 / 87bbb068ebb6e289f9c5e01c89a144d6ba8bd58897ee303e5600e0fe964131e5
2 / The unsuccessful fix
Exit 1"""Failure Map reference implementation. Python standard library only."""
import json
import math
import struct
def render(x):
if math.isnan(x): return 'nan'
if math.isinf(x): return '-infinity' if x<0 else '+infinity'
return format(x,'.11g')
N = 1
observations = []
def solve(x,y):
try:
if y==0: return 'domain'
ay=abs(y)
r=abs(math.fmod(x,ay))
a=abs(r)
other=ay-a
odd=math.fmod(abs(x),2*ay)>=ay
if a>other or (a==other and odd):
r-=math.copysign(ay,x)
if r==0: r=math.copysign(0.0,x)
return render(r)
except (ValueError, OverflowError, ZeroDivisionError, TypeError):
return "arithmetic-error"
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('even tie', solve(2.0*N*4+2,4.0), "2")
check('odd tie', solve((2.0*N+1)*4+2,4.0), "-2")
check('negative even tie', solve(-2.0*N*4-2,4.0), "-2")
check('negative odd tie', solve(-(2.0*N+1)*4-2,4.0), "2")
check('above half', solve(4.0*N+3,4.0), "-1")
check('below half', solve(4.0*N+1,4.0), "1")
check('negative divisor below half', solve(4.0*N+1,-4.0), "1")
check('negative divisor', solve(4.0*N+3,-4.0), "-1")
check('negative exact', solve(-4.0*N,4.0), "-0")
check('positive exact', solve(4.0*N,4.0), "0")
check('zero divisor', solve(float(N),0.0), "domain")
check('huge finite', solve(1.5e308,1e308), render(math.remainder(1.5e308,1e308)))
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| even tie | 2 | 2 | Passed |
| odd tie | -2 | -2 | Passed |
| negative even tie | 2 | -2 | Failed |
| negative odd tie | 6 | 2 | Failed |
| above half | -1 | -1 | Passed |
| below half | 1 | 1 | Passed |
| negative divisor below half | 1 | 1 | Passed |
| negative divisor | -1 | -1 | Passed |
| negative exact | -0 | -0 | Passed |
| positive exact | 0 | 0 | Passed |
| zero divisor | domain | domain | Passed |
| huge finite | -5e+307 | -5e+307 | Passed |
SHA-256 / fc03c94320f7e940ae96f459d1db0e71fc4b548b8d0ec8d5e5cc91c017236396
3 / The verified repair
Exit 0"""Failure Map reference implementation. Python standard library only."""
import json
import math
import struct
def render(x):
if math.isnan(x): return 'nan'
if math.isinf(x): return '-infinity' if x<0 else '+infinity'
return format(x,'.11g')
N = 1
observations = []
def solve(x,y):
try:
if y==0: return 'domain'
ay=abs(y)
r=math.fmod(x,ay)
a=abs(r)
other=ay-a
odd=math.fmod(abs(x),2*ay)>=ay
if a>other or (a==other and odd):
r-=math.copysign(ay,x)
if r==0: r=math.copysign(0.0,x)
return render(r)
except (ValueError, OverflowError, ZeroDivisionError, TypeError):
return "arithmetic-error"
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
check('even tie', solve(2.0*N*4+2,4.0), "2")
check('odd tie', solve((2.0*N+1)*4+2,4.0), "-2")
check('negative even tie', solve(-2.0*N*4-2,4.0), "-2")
check('negative odd tie', solve(-(2.0*N+1)*4-2,4.0), "2")
check('above half', solve(4.0*N+3,4.0), "-1")
check('below half', solve(4.0*N+1,4.0), "1")
check('negative divisor below half', solve(4.0*N+1,-4.0), "1")
check('negative divisor', solve(4.0*N+3,-4.0), "-1")
check('negative exact', solve(-4.0*N,4.0), "-0")
check('positive exact', solve(4.0*N,4.0), "0")
check('zero divisor', solve(float(N),0.0), "domain")
check('huge finite', solve(1.5e308,1e308), render(math.remainder(1.5e308,1e308)))
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| even tie | 2 | 2 | Passed |
| odd tie | -2 | -2 | Passed |
| negative even tie | -2 | -2 | Passed |
| negative odd tie | 2 | 2 | Passed |
| above half | -1 | -1 | Passed |
| below half | 1 | 1 | Passed |
| negative divisor below half | 1 | 1 | Passed |
| negative divisor | -1 | -1 | Passed |
| negative exact | -0 | -0 | Passed |
| positive exact | 0 | 0 | Passed |
| zero divisor | domain | domain | Passed |
| huge finite | -5e+307 | -5e+307 | Passed |
SHA-256 / 219b3658834515af71ddcc114790af2e7ee62879adddbee7d0d062833b3403a6
Verification & scope
Controlled binary64 or explicitly stipulated miniature format; no hardware exception flags or platform floating environment are modeled. This reproducer isolates one failure mechanism. Results cover the supplied fixtures. Variants within a family share a test contract and should remain grouped when constructing evaluation splits. Related mechanisms with a shared evaluation_group must also remain together; these controlled models are not independent production incidents.
Observations recorded using Python 3.12.14 at 2026-09-29T14:39:45.051734+00:00.
Case digest / 943601ec98a200e5e9627d0ec8b3fca3d736423a41a4dfd0e6956cc0312e7d01