Mark, these are the "spirit of 754" rules:
1. If the mathematical result is a real number, but of magnitude too
large to approximate by a machine float, overflow is signaled and the
result is an infinity (with the appropriate sign).
2. If the mathematical result is a real number, but of magnitude too
small to approximate by a machine float, underflow is signaled and the
result is a zero (with the appropriate sign).
3. At a singularity (a value x such that the limit of f(y) as y
approaches x exists and is an infinity), "divide by zero" is signaled
and the result is an infinity (with the appropriate sign). This is
complicated a little by that the left-side and right-side limits may not
be the same; e.g., 1/x approaches +inf or -inf as x approaches 0 from
the positive or negative directions. In that specific case, the sign of
the zero determines the result of 1/0.
4. At a point where a function has no defined result in the extended
reals (i.e., the reals plus an infinity or two), invalid operation is
signaled and a NaN is returned.
And these are what Python has historically /tried/ to do (but not always
successfully, as platform libm behavior varies a lot):
For #1, raise OverflowError.
For #2, return a zero (with the appropriate sign if that happens by
accident ;-)).
For #3 and #4, raise ValueError. It may have made sense to raise
Python's ZeroDivisionError in #3, but historically that's only been
raised for division by zero and mod by zero.
You're right that you can't make everyone happy, so may as well stick
with historical consistency, and wait for a PEP to introduce a more
flexible mechanism. As W. Kahan wrote long ago, the reason they're
called "exceptions" is that no matter which fixed policy you adopt,
someone will take strident exception to it ;-) |