Skip to content

Commit

Permalink
make consistency.Approx repr consistent with class behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
pressler-vsc committed Jun 6, 2023
1 parent 1faf4a7 commit 2cfc6ed
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
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"

0 comments on commit 2cfc6ed

Please sign in to comment.