Skip to content

Commit e15f9ba

Browse files
fix: mathematical approximations leading to potential issues - allowing rounding (#2190)
Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com>
1 parent 207ed74 commit e15f9ba

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

doc/changelog.d/2190.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Mathematical approximations leading to potential issues - allowing rounding

src/ansys/geometry/core/math/misc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ def get_two_circle_intersections(
8080
return None
8181
else:
8282
a = (r0**2 - r1**2 + d**2) / (2 * d)
83-
h = np.sqrt(r0**2 - a**2)
83+
# abs is used to ensure non-negative value - close to zero
84+
# conditions might occur if circles are nearly tangent
85+
h = np.sqrt(abs(r0**2 - a**2))
8486
x2 = x0 + a * (x1 - x0) / d
8587
y2 = y0 + a * (y1 - y0) / d
8688

src/ansys/geometry/core/sketch/sketch.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,11 @@ def arc_from_start_end_and_radius(
516516
-------
517517
Sketch
518518
Revised sketch state ready for further sketch actions.
519+
520+
Warnings
521+
--------
522+
This method may not produce the exact same radius, depending on the input parameters.
523+
This is due to the mathematical approximations involved in the arc creation.
519524
"""
520525
arc = Arc.from_start_end_and_radius(
521526
start,

tests/integration/test_issues.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from ansys.geometry.core.misc import DEFAULT_UNITS, UNITS, Angle, Distance
4141
from ansys.geometry.core.modeler import Modeler
4242
from ansys.geometry.core.sketch import Sketch
43+
from ansys.geometry.core.sketch.arc import Arc
4344
from ansys.geometry.core.tools import (
4445
MeasurementTools,
4546
PrepareTools,
@@ -371,3 +372,25 @@ def test_issue_2184_prevent_raw_instantiation_of_tools_and_commands():
371372
GeometryRuntimeError, match="GeometryCommands should not be instantiated directly"
372373
):
373374
GeometryCommands(None)
375+
376+
377+
def test_issue_2074_rounding_math_errors():
378+
"""Test that rounding errors in math operations do not cause issues.
379+
380+
For more info see
381+
https://github.com/ansys/pyansys-geometry/issues/2074
382+
"""
383+
sketch1 = Sketch()
384+
width = 7
385+
start = Point2D([width / 2, 0], UNITS.mm)
386+
end = Point2D([-width / 2, 0], UNITS.mm)
387+
radius = width / 2 * UNITS.mm
388+
sketch1.arc_from_start_end_and_radius(start, end, radius)
389+
390+
arc: Arc = sketch1.edges[-1]
391+
392+
assert arc.center is not None
393+
assert np.allclose(arc.center, Point2D([0, 0], UNITS.mm))
394+
assert np.isclose(arc.radius.to_base_units().m, radius.to_base_units().m)
395+
assert arc.start == start
396+
assert arc.end == end

0 commit comments

Comments
 (0)