About Rational Number (PEP 239/PEP 240)
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sat Dec 15 19:42:56 EST 2007
On Sat, 15 Dec 2007 15:44:26 -0800, Arnaud Delobelle wrote:
> Rationals are not that simple.
>
> * Unless you are working under very controlled conditions, rationals
> very quickly grow enormous numerators and denominators, hence require
> arbitrary precision integers (which, I concede, are part of Python).
Come now. Rationals aren't living things that grow if you sit them in the
corner and provide them air and light. Whether the numerator and
denominator grow depends on what you do with them. If you wish to say
that they *can* grow enormous numerators and denominators, I won't argue,
but implying that *must* and will *always* grow is misleading. It's pure
propaganda.
And if they do... well, longs can also "quickly grow enormous". That
hasn't stopped Python integrating ints and longs. The programmer is
expected to deal with it. Integrating floats and rationals isn't even on
the table for discussion. Anyone using rationals is making a conscious
choice to do so and can deal with the consequences.
> * In order to have a canonical representation of integers, they need to
> be kept in normalised form.
Oh noes! Not normalised form!!!
Is this supposed to be an objection?
> * Nobody uses big fractions apart from mathematicians (and maybe
> bookmakers?).
Putting the cart before the horse. Nobody uses rationals because
rationals aren't available!
Nobody uses complex numbers except for mathematicians, and maybe a few
electrical engineers, and they're a built in. Nobody uses math functions
like sin and cos except for mathematicians and engineers, and they're
available.
The Forth programming language didn't even support floating point for
many years. Programmers were expected to use the equivalent of rationals
using integers. This was very successful, and avoided the gotchas that
you get with floats. For example, with floats it is unavoidable to have
unintuitive results like (x + y) - x != y and it isn't even very hard to
find an example.
>>> 3.1 + 0.7 - 3.1 == 0.7
False
Such a bizarre result shouldn't happen with any non-buggy rational
representation:
31/10 + 7/10 - 31/10 => 38/10 - 31/10 => 7/10
In the real world, people use fractions all the time, e.g. plumbers. (Not
that I'm expecting plumbers to start taking laptops out to the building
site in order to calculate pipe sizes.)
> * Having yet another numerical type would make it difficult what type to
> expect from a calculation.
I simply don't believe that at all. It's not like calculations will
suddenly start returning rationals unexpectedly, any more than they
suddenly started returning Decimals unexpectedly.
I find it bizarre that the simple to implement, simple to understand
rational data type was rejected while the hard to implement, hard to
understand Decimal was accepted.
--
Steven
More information about the Python-list
mailing list