I am writing a simple app that takes a bunch of numerical inputs and calculates a set of results. (The app is in PyGTK but I don't think that's relevant.)
My problem is that if I want to just have NaN's and Inf's propagated through, then in every calculation I need to do something like:
# At the top of the module
nan = float("nan")
inf = float("inf")
try:
res = (a + b) / (0.1*c + d)
except ZeroDivisionError:
# replicate every little subtlety of IEEE 754 here
except OverflowError:
# replicate every little subtlety of IEEE 754 here again
...or, of course, pre-empt it for every calculation:
numerator = a + b
denominator = 0.1*c + d
if denominator == 0:
# etc
elif math.isnan(numerator):
# *sigh*
How can I deal with this sanely in Python 2.6? Do I really need to install a massive 3rd-party module (numpy, scipy) on every target machine just to do IEEE 754 arithmetic? Or is there a simpler way?