Skip to content

cmath.isclose incorrectly returns True for large finite opposite complex values #158697

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: Python standard-library documentation, cmath.isclose: "If no errors occur, the result will be: abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)."

Expected: False

Actual: True

import cmath
import math
from fractions import Fraction as F

p = dict(a=complex(1.7e308, 1.7e308), b=complex(-1.7e308, -1.7e308),
         rel_tol=1e-9, abs_tol=0.0)
a, b = p["a"], p["b"]
if not (all(map(math.isfinite, (a.real, a.imag, b.real, b.imag,
                                p["rel_tol"], p["abs_tol"])))
        and p["rel_tol"] >= 0 and p["abs_tol"] >= 0):
    print("REFUTATION REJECTED:", "invalid input", p)
else:
    x, y, u, v = map(F, (a.real, a.imag, b.real, b.imag))
    expected = (x-u)**2 + (y-v)**2 <= max(
        F(p["rel_tol"])**2 * max(x*x+y*y, u*u+v*v),
        F(p["abs_tol"])**2)
    actual = cmath.isclose(**p)
    if actual != expected:
        print("REFUTATION CONFIRMED:", p, "actual:", actual, "expected:", expected)
    else:
        print("REFUTATION REJECTED:", "actual agrees with exact expectation",
              p, "actual:", actual, "expected:", expected)

Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library cmath:

REFUTATION CONFIRMED: {'a': (1.7e+308+1.7e+308j), 'b': (-1.7e+308-1.7e+308j), 'rel_tol': 1e-09, 'abs_tol': 0.0} actual: True expected: False

This report was found and written by an automated property-testing tool I run (bugforge). The reproducer above was executed and its output is pasted unedited; no person reviewed the report before it was filed. The search script is in https://github.com/augusto-rehfeldt/bugforge-results/tree/main/cmath-20261003-033712-c2

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions