Rational Numbers

Nick Maclaren nmm1 at cus.cam.ac.uk
Thu Jan 11 18:47:17 EST 2007


In article <mailman.2597.1168550439.32031.python-list at python.org>,
Facundo Batista <facundo at taniquetil.com.ar> writes:
|> Noud Aldenhoven wrote:
|> 
|> > When I was programming in a mathematical project I began to wonder if python
|> > supports rational numbers[1]. In a language like magma[2] it's not such a
|> > problem. Does python supports something simular?
|> 
|> Python does not have rational numbers.
|> 
|> There's a (rejected) PEP about this, PEP-239:
|> 
|>   http://www.python.org/dev/peps/pep-0239/
|> 
|> Maybe you also want to read the section "Why not rational?" of
|> PEP-327...
|> 
|>   http://www.python.org/dev/peps/pep-0327/#why-not-rational

Oh, God.  Decimal delusions again :-(

There are good arguments for not having a rational type, but the idea
that decimal floating-point is in any way a replacement is cuckoo.
The desirable properties that rationals and decimal arithmetic provide
are both genuine and are almost entirely different ....

There are also perfectly good representations that don't have the
performance implications mentioned:  fixed and floating slash arithmetic.
They are mathematically quite interesting, and have some desirable
properties not shared by any floating-point format: e.g. if the result
is theoretically a rational with small numerator and denominator, then
they are very likely to return it.  I implemented fixed slash once, and
it is very easy to do - all it needs is a GCD routine - like rational,
it would be straightforward to add to Python as an extension type.


Regards,
Nick Maclaren.



More information about the Python-list mailing list