Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix floating comparison in path.arc #26991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/matplotlib/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,13 +943,20 @@ def arc(cls, theta1, theta2, n=None, is_wedge=False):
polylines, quadratic or cubic Bezier curves
<https://web.archive.org/web/20190318044212/http://www.spaceroots.org/documents/ellipse/index.html>`_.
"""
# we want to get the same result when the input changes within [-tol, tol]
# this behavior is only for internal use to offset floating-point error
# so it should not be relied on by the user
tol = 1e-6

halfpi = np.pi * 0.5

eta1 = theta1
eta2 = theta2 - 360 * np.floor((theta2 - theta1) / 360)
# Ensure 2pi range is not flattened to 0 due to floating-point errors,
# but don't try to expand existing 0 range.
if theta2 != theta1 and eta2 <= eta1:
# condition: theta1 != theta2 and eta2 <= eta1
# implementation note: use np.isclose() may break when theta1 = 0
if abs(theta1 - theta2) >= tol and (eta2 <= eta1 + tol):
eta2 += 360
eta1, eta2 = np.deg2rad([eta1, eta2])

Expand Down
16 changes: 16 additions & 0 deletions lib/matplotlib/tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,22 @@ def test_full_arc(offset):
np.testing.assert_allclose(maxs, 1)


@pytest.mark.parametrize('offset', range(-720, 361, 45))
def test_full_arc_rounding(offset):
# GH 26972 - edge case for floating point rounding
# we want get the same result when input of arc
# changes within [-tol, tol], where tol = 1e-6
tol = 1e-6
low = offset
high = 360 + offset + tol

path = Path.arc(low, high)
mins = np.min(path.vertices, axis=0)
maxs = np.max(path.vertices, axis=0)
np.testing.assert_allclose(mins, -1)
np.testing.assert_allclose(maxs, 1)


def test_disjoint_zero_length_segment():
this_path = Path(
np.array([
Expand Down