Skip to content

Commit

Permalink
Fix range error bug due to rounding error (#27, #67)
Browse files Browse the repository at this point in the history
  • Loading branch information
moshi4 committed May 18, 2024
1 parent 9a39fbc commit 61a8534
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/pycirclize/sector.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,17 @@ def x_to_rad(self, x: float, ignore_range_error: bool = False) -> float:
rad : float
Radian coordinate
"""
if not self.start <= x <= self.end and not ignore_range_error:
err_msg = f"{x=} is invalid range of '{self.name}' sector.\n{self}"
raise ValueError(err_msg)
#
# Check target x is in valid sector range
if not ignore_range_error:
# Apply relative torelance value to sector range to avoid
# unexpected invalid range error due to rounding errors (Issue #27, #67)
rel_tol = 1e-14
min_range, max_range = self.start - rel_tol, self.end + rel_tol
if not min_range <= x <= max_range:
err_msg = f"{x=} is invalid range of '{self.name}' sector.\n{self}"
raise ValueError(err_msg)

if not self.clockwise:
x = (self.start + self.end) - x
size_ratio = self.rad_size / self.size
Expand Down

0 comments on commit 61a8534

Please sign in to comment.