FAILURE MAP
← Case archive

FA-17331 / Floating-point arithmetic / Open access

Nearest floating remainder compares by doubling a huge residue · case 01

Nearest floating remainder compares by doubling a huge residue.

Verified by executionVariant 1 · 12 checks per implementationDownload source bundle ↓JSON ↗

ROOT CAUSE

Nearest floating remainder compares by doubling a huge residue. The faulty expression is other=ay/2; a=2*a.

VERIFIED REPAIR

Apply the contract at this fault site using other=ay-a.

Unsuccessful approach: The attempted local correction other=ay-2*a 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=math.fmod(x,ay)
        a=abs(r)
        other=ay/2; a=2*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 fixtureActualExpectedOutcome
even tie-22Failed
odd tie-2-2Passed
negative even tie2-2Failed
negative odd tie22Passed
above half-1-1Passed
below half-31Failed
negative divisor below half-31Failed
negative divisor-1-1Passed
negative exact-0-0Passed
positive exact00Passed
zero divisordomaindomainPassed
huge finite-5e+307-5e+307Passed

SHA-256 / 46e386f70cd9ce8cd67ef3e3668f1b2a3ef51e6209f410d37fbd37423d148ea2

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=math.fmod(x,ay)
        a=abs(r)
        other=ay-2*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 fixtureActualExpectedOutcome
even tie-22Failed
odd tie-2-2Passed
negative even tie2-2Failed
negative odd tie22Passed
above half-1-1Passed
below half11Passed
negative divisor below half11Passed
negative divisor-1-1Passed
negative exact-0-0Passed
positive exact00Passed
zero divisordomaindomainPassed
huge finite-5e+307-5e+307Passed

SHA-256 / f9b17682e9db93d8fbda81bb3526d8818199ffcfca50a4429d4273e0d6c70cc4

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 fixtureActualExpectedOutcome
even tie22Passed
odd tie-2-2Passed
negative even tie-2-2Passed
negative odd tie22Passed
above half-1-1Passed
below half11Passed
negative divisor below half11Passed
negative divisor-1-1Passed
negative exact-0-0Passed
positive exact00Passed
zero divisordomaindomainPassed
huge finite-5e+307-5e+307Passed

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.100645+00:00.

Case digest / a466d54b340a16e896e3cd43f39681a8c9558250c2ced63c7d5140b6ab154ca2