You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.9 KiB

2 months ago
  1. import math
  2. import sys
  3. class Infinity:
  4. """
  5. Class representing infinity and minus infinity
  6. """
  7. def __init__(self, negated = False):
  8. self.negated = negated
  9. def __neg__(self):
  10. return Infinity(not self.negated)
  11. def __le__(self, other):
  12. if isinstance(other, Infinity) and (not other.negated or other.negated == self.negated): return True
  13. return self.negated
  14. def __ge__(self, other):
  15. if isinstance(other, Infinity) and (other.negated or other.negated == self.negated): return True
  16. return not self.negated
  17. def __lt__(self, other):
  18. if isinstance(other, Infinity) and other.negated: return False
  19. return self.negated
  20. def __gt__(self, other):
  21. if isinstance(other, Infinity) and not other.negated: return False
  22. return not self.negated
  23. def __eq__(self, other):
  24. if sys.version_info[1] >= 5:
  25. return (isinstance(other, Infinity) and other.negated == self.negated) or (not self.negated and other == math.inf)
  26. else:
  27. return (isinstance(other, Infinity) and other.negated == self.negated)
  28. def __add__(self, other):
  29. return self
  30. def __radd__(self, other):
  31. return self
  32. def __sub__(self, other):
  33. return self
  34. def __rsub__(self, other):
  35. return self
  36. def __mul__(self, other):
  37. if other < 0:
  38. return -self
  39. return self
  40. def __rmul__(self, other):
  41. if other < 0:
  42. return -self
  43. return self
  44. def __div__(self, other):
  45. if other < 0:
  46. return - self
  47. return self
  48. def __rdiv__(self, other):
  49. return 0
  50. def __str__(self):
  51. return "-infty" if self.negated else "infty"
  52. def __repr__(self):
  53. return "-pycarl.inf" if self.negated else "pycarl.inf"
  54. def __hash__(self):
  55. return 42 if self.negated else 23