Skip to content

Commit

Permalink
ligotimegps: handle infinities
Browse files Browse the repository at this point in the history
LIGOTimeGPS can't represent infinity, so we need to catch them in comparisons and equality checks and just declare the result
  • Loading branch information
duncanmmacleod committed Jan 16, 2018
1 parent 2e54293 commit 39df286
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion ligotimegps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# You should have received a copy of the GNU General Public License
# along with ligotimegps. If not, see <http://www.gnu.org/licenses/>.

from math import (modf, log)
from math import (modf, log, isinf)
from functools import wraps
from decimal import Decimal

Expand Down Expand Up @@ -171,6 +171,8 @@ def ns(self):
# -- comparison -----------------------------

def __eq__(self, other):
if isinf(other):
return False
if not isinstance(other, LIGOTimeGPS):
other = LIGOTimeGPS(other)
return (self._seconds == other._seconds and
Expand All @@ -180,6 +182,10 @@ def __ne__(self, other):
return not (self == other)

def __lt__(self, other):
if isinf(other) and other > 0: # +infinity
return True
if isinf(other): # -infinity
return False
if not isinstance(other, LIGOTimeGPS):
other = LIGOTimeGPS(other)
if self._seconds < other._seconds:
Expand Down
7 changes: 7 additions & 0 deletions ligotimegps/test_ligotimegps.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,10 @@ def test_abs():
a = LIGOTimeGPS(123, 456789)
assert abs(a) == a
assert abs(-a) == a


def test_infinity():
assert LIGOTimeGPS(1) < float('inf')
assert LIGOTimeGPS(1) > -float('inf')
assert LIGOTimeGPS(1) != float('inf')
assert LIGOTimeGPS(1) != -float('inf')

0 comments on commit 39df286

Please sign in to comment.