Skip to content

Handle infinite values in the C isclose() like math.isclose() - #1487

Closed
zzoraemon043 wants to merge 1 commit into
mozman:masterfrom
zzoraemon043:fix/c-isclose-infinity
Closed

zzoraemon043 wants to merge 1 commit into
mozman:masterfrom
zzoraemon043:fix/c-isclose-infinity

Conversation

@zzoraemon043

Copy link
Copy Markdown
Contributor

The isclose() helper in acc/vector.pyx is commented "Has to match the Python implementation!", but it skips the infinity checks of math.isclose(). With the C extension:

import math
from ezdxf.math import Vec3

Vec3(0, 0, 0).isclose((math.inf, 0, 0))        # True,  Python: False
Vec3(math.inf, 0, 0).isclose((-math.inf, 0, 0)) # True,  Python: False
Vec3(math.inf, 0, 0).isclose((math.inf, 0, 0))  # False, Python: True

inf - x is inf, and rel_tol * inf is also inf, so diff <= fabs(rel_tol * b) passes; for two equal infinities diff is nan. Vec2 behaves the same, and the C construct, bezier and bspline code share this helper.

The fix adds the two early checks from CPython's math.isclose(): equal values are close, and an infinite value is only close to itself. Finite inputs give the same results as before.

Added tests to suites 602 and 603, so they run against both implementations: the C variants fail before the fix and pass after.

pytest tests (C extension rebuilt): 8225 passed, 28 skipped.

The C helper returned True for a finite value compared to an infinite
value (and for inf vs -inf), and False for two equal infinite values.
Vec2.isclose() and Vec3.isclose() of the C extension inherited this.
@mozman

mozman commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

In this case: NO - this is nasty but in CAD applications infinite values are not expected.

EDIT: There will be no end to it if I open this Pandora’s box!

@mozman mozman closed this Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants