scripts: csv.py: Fixed divide-by-zero, return +-inf

This may make some mathematician mad, but these are informative scripts.
Returning +-inf is much more useful than erroring when dealing with
several hundred rows of results.

And hey, if it's good enough for IEEE 754, it's good enough for us :)

Also fixed a division operator mismatch in RFrac that was causing
problems.
This commit is contained in:
Christopher Haster
2024-11-13 15:21:04 -06:00
parent 5dc9eabbf7
commit 2fa968dd3f
8 changed files with 47 additions and 2 deletions

View File

@@ -114,6 +114,11 @@ class RInt(co.namedtuple('RInt', 'x')):
return self.__class__(self.x * other.x)
def __truediv__(self, other):
if not other:
if self >= self.__class__(0):
return self.__class__(+mt.inf)
else:
return self.__class__(-mt.inf)
return self.__class__(self.x // other.x)
def __mod__(self, other):
@@ -207,6 +212,11 @@ class RFloat(co.namedtuple('RFloat', 'x')):
return self.__class__(self.x * other.x)
def __truediv__(self, other):
if not other:
if self >= self.__class__(0):
return self.__class__(+mt.inf)
else:
return self.__class__(-mt.inf)
return self.__class__(self.x / other.x)
def __mod__(self, other):
@@ -279,7 +289,7 @@ class RFrac(co.namedtuple('RFrac', 'a,b')):
return self.__class__(self.a * other.a, self.b * other.b)
def __truediv__(self, other):
return self.__class__(self.a // other.a, self.b // other.b)
return self.__class__(self.a / other.a, self.b / other.b)
def __mod__(self, other):
return self.__class__(self.a % other.a, self.b % other.b)