From 2fa968dd3f614622541a90e309607bd9441e2227 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Wed, 13 Nov 2024 15:21:04 -0600 Subject: [PATCH] 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. --- scripts/code.py | 5 +++++ scripts/cov.py | 7 ++++++- scripts/csv.py | 12 +++++++++++- scripts/data.py | 5 +++++ scripts/perf.py | 5 +++++ scripts/perfbd.py | 5 +++++ scripts/stack.py | 5 +++++ scripts/structs.py | 5 +++++ 8 files changed, 47 insertions(+), 2 deletions(-) diff --git a/scripts/code.py b/scripts/code.py index b22973a1..f78dfaea 100755 --- a/scripts/code.py +++ b/scripts/code.py @@ -120,6 +120,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): diff --git a/scripts/cov.py b/scripts/cov.py index ef600a20..46261b23 100755 --- a/scripts/cov.py +++ b/scripts/cov.py @@ -121,6 +121,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): @@ -193,7 +198,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) diff --git a/scripts/csv.py b/scripts/csv.py index 5c65d63c..17cb3bf2 100755 --- a/scripts/csv.py +++ b/scripts/csv.py @@ -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) diff --git a/scripts/data.py b/scripts/data.py index 25351106..fa820bc6 100755 --- a/scripts/data.py +++ b/scripts/data.py @@ -120,6 +120,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): diff --git a/scripts/perf.py b/scripts/perf.py index 0eee8be7..539c0934 100755 --- a/scripts/perf.py +++ b/scripts/perf.py @@ -129,6 +129,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): diff --git a/scripts/perfbd.py b/scripts/perfbd.py index 3ee67b47..1a5b81de 100755 --- a/scripts/perfbd.py +++ b/scripts/perfbd.py @@ -120,6 +120,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): diff --git a/scripts/stack.py b/scripts/stack.py index ef1090f0..1ae7bf0f 100755 --- a/scripts/stack.py +++ b/scripts/stack.py @@ -110,6 +110,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): diff --git a/scripts/structs.py b/scripts/structs.py index 50d12c97..043e4303 100755 --- a/scripts/structs.py +++ b/scripts/structs.py @@ -115,6 +115,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):