[Python-Dev] PEP239 (Rational Numbers) Reference Implementation and new issues

John Roth johnroth at ameritech.net
Fri Oct 4 10:42:57 EDT 2002


"Chad Netzer" <cnetzer at mail.arc.nasa.gov> wrote in message
news:mailman.1033702569.6466.python-list at python.org...
>
> My comments about 0/0 were somewhat tongue-in-cheek, so don't take
them
> too seriously. :)
>
> As Tim Peters pointed out, there is often a convention in the
numerical
> field to declare (for the purposes of floating point calculation)
that:
>
>    1.0/0.0 = Inf
>    0.0/0.0 = NaN
>
> A useful question is what to do with the equivalent rational
> expressions?  Should the floating point conventions be followed?
> Should there even be the equivalent of a NaN or Inf?  Do they apply
> only when memory limits, or lossy conversions, come into play?

Well, my first impression was that when you get into the limit thing,
you're talking about mathematics that is usually applied to physics or
other essentially continuous quantities, not discrete quantities.

> My gut instinct is that both 1/0 and 0/0 should simply throw divide by
> zero, and that NaN and Inf do not exist.  If they do exist, they are
> defined directly by subclasses of the rational type (and are NOT
> created by any simple, defined expression, only by practical limits).

> At least that should be the default; like floating point, there may be
> a need to override the exception throwing behavior (for array
> operations, etc.)  But this complicates things, and since these are
not
> performance critical, I think it best to keep things simple and
> consistent.  Those working with large numbers of rationals may just
> have to deal with the possibility of exceptions, and perhaps
> precondition there data.  (A compromise might be a DividedByZero
> object, that is distinct from a Nan or Inf)

I think keeping it simple is exactly right. I don't have any clear
idea of how to make it values like NaN and Inf 'right,' so I'd go
with just throwing an exception for anything that attempts to get
a value from a rational with a zero denominator. If someone needs
a different behavior, they can encapsulate it and catch the (hopefully
rare) exception.

John Roth







More information about the Python-list mailing list