scripts: Tweaked RFrac to return +-∞ when evaluated as a float

This affects the table renderers as well as csv.py's ratio expr.

This is a bit more correct, handwaving 0/0 (mapping 0/0 -> 100% is
useful for cov.py, please don't kill me mathematicians):

  frac(1,0) => 1/0 (∞%)
  frac(0,0) => 0/0 (100.0%)
  frac(0,1) => 0/1 (0.0%)
This commit is contained in:
Christopher Haster
2025-04-12 01:00:04 -05:00
parent 613fa0f27a
commit 26a29bda31
2 changed files with 15 additions and 3 deletions

View File

@@ -261,7 +261,12 @@ class RFrac(co.namedtuple('RFrac', 'a,b')):
return '%11s' % (self,)
def notes(self):
t = self.a.x/self.b.x if self.b.x else 1.0
if self.b.x == 0 and self.a.x == 0:
t = 1.0
elif self.b.x == 0:
t = mt.copysign(mt.inf, self.a.x)
else:
t = self.a.x / self.b.x
return ['%' if t == +mt.inf
else '-∞%' if t == -mt.inf
else '%.1f%%' % (100*t)]
@@ -735,8 +740,10 @@ class RExpr:
def eval(self, fields={}):
v = RFrac(self.a.eval(fields))
if not float(v.b):
if not float(v.b) and not float(v.a):
return RFloat(1)
elif not float(v.b):
return RFloat(mt.copysign(mt.inf, float(v.a)))
else:
return RFloat(float(v.a) / float(v.b))