Turn off ZeroDivisionError?
Carl Banks
pavlovevidence at gmail.com
Sun Feb 10 19:08:22 EST 2008
On Feb 10, 3:29 pm, Grant Edwards <gra... at visi.com> wrote:
> On 2008-02-10, Mark Dickinson <dicki... at gmail.com> wrote:
>
> > On Feb 9, 5:03 pm, Neal Becker <ndbeck... at gmail.com> wrote:
> >> If I use C code to turn off the hardware signal, will that stop python from
> >> detecting the exception, or is python checking for 0 denominator on it's
> >> own (hope not, that would waste cycles).
>
> > Yes, Python does do an explicit check for a zero denominator. Here's
> > an excerpt from floatdiv.c in Objects/floatobject.c:
>
> > if (b == 0.0) {
> > PyErr_SetString(PyExc_ZeroDivisionError, "float division");
> > return NULL;
> > }
>
> > This is probably the only sane way to deal with differences in
> > platform behaviour when doing float divisions.
>
> I've always found that check to be really annoying. Every time
> anybody asks about floating point handling, the standard
> response is that "Python just does whatever the underlying
> platform does". Except it doesn't in cases like this. All my
> platforms do exactly what I want for division by zero: they
> generate a properly signed INF. Python chooses to override
> that (IMO correct) platform behavior with something surprising.
> Python doesn't generate exceptions for other floating point
> "events" -- why the inconsistency with divide by zero?
I understand your pain, but Python, like any good general-purpose
language, is a compromise. For the vast majority of programming,
division by zero is a mistake and not merely a degenerate case, so
Python decided to treat it like one.
Carl Banks
More information about the Python-list
mailing list