Float compression [Re: floating point math results question]

Jonathan Hogg jonathan at onegoodidea.com
Sun Jan 27 11:26:53 EST 2002


On 27/1/2002 14:01, in article
mailman.1012140225.20171.python-list at python.org, "François Pinard"
<pinard at iro.umontreal.ca> wrote:

> I may send you the one I have, just ask! :-) In fact, my constructor for
> the rational type does a fractional approximation, if given float arguments.

I posted with the sure and certain knowledge that someone somewhere has
implemented a rational class for Python, probably multiple versions ;-)

> Be aware that rational computations are _not_ memory economical, you might
> get numerators and denominators to grow very quickly

Precisely why I argued that memory economy probably doesn't matter for most
people. If you store something as two large numbers, it's hard to use less
space than that which two large numbers occupy. The point of using rational
numbers isn't to be efficient, it's to be exact in circumstances where a
floating point number isn't (i.e., almost all base-10 arithmetic).

> I would guess that `str()' has been implemented with some care about
> efficiency.  Couldn't you just use it?

However, here you've lost me. You've written a rational type, but you can
only guess that str() is efficient on it? Didn't you write the __str__ hook?
Or do you implicitly convert to float and let normal str operate on that?
(which kind of defeats the purpose of having an exact representation if you
then convert to an approximate.)

Jonathan




More information about the Python-list mailing list