Skip to content
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

Bugfix: make consistency.Approx repr consistent with class behavior #427

Merged
merged 1 commit into from Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions sarpy/consistency/consistency.py
Expand Up @@ -418,25 +418,26 @@ def __lt__(self, rhs):
return self.__le__(rhs)

def __le__(self, rhs):
return np.all(np.logical_or(np.less(self.value, rhs),
np.isclose(self.value, rhs, rtol=self.rtol, atol=self.atol)))
return np.all(np.logical_or(np.less(self.value, rhs), self._isclose(rhs)))

def __eq__(self, rhs):
return np.all(np.isclose(self.value, rhs, rtol=self.rtol, atol=self.atol))
return np.all(self._isclose(rhs))

def __ne__(self, rhs):
return not self.__eq__(rhs)

def __ge__(self, rhs):
return np.all(np.logical_or(np.greater(self.value, rhs),
np.isclose(self.value, rhs, rtol=self.rtol, atol=self.atol)))
return np.all(np.logical_or(np.greater(self.value, rhs), self._isclose(rhs)))

def __gt__(self, rhs):
return self.__ge__(rhs)

def __repr__(self):
tol = np.maximum(self.atol, np.asarray(self.value) * self.rtol)
return "{} +/- {}".format(self.value, tol)
tol = self.atol + np.abs(np.asarray(self.value)) * self.rtol
return f"{self.value} ± {tol}"

def _isclose(self, rhs):
return np.isclose(rhs, self.value, rtol=self.rtol, atol=self.atol)


def in_color(string, *color):
Expand Down
4 changes: 2 additions & 2 deletions tests/consistency/test_consistency.py
Expand Up @@ -161,7 +161,7 @@ def test_invalid(dummycon):


def test_approx():
apx = con.Approx(10.0, atol=.1)
apx = con.Approx(10.0, atol=.1, rtol=0)
assert apx == 10.0
assert apx == 10.01
assert not apx != 10.01
Expand All @@ -171,4 +171,4 @@ def test_approx():
assert not apx <= 0
assert apx < 10.01
assert apx <= 10.01
assert repr(apx) == "10.0 +/- 0.1"
assert repr(apx) == "10.0 ± 0.1"