2001-03-15 23:27:13 -05:00
|
|
|
|
PEP: 240
|
|
|
|
|
Title: Adding a Rational Literal to Python
|
|
|
|
|
Version: $Revision$
|
|
|
|
|
Author: pep@zadka.site.co.il (Moshe Zadka)
|
|
|
|
|
Status: Draft
|
|
|
|
|
Type: Standards Track
|
|
|
|
|
Created: 11-Mar-2001
|
|
|
|
|
Python-Version: 2.2
|
2001-03-16 11:02:24 -05:00
|
|
|
|
Post-History: 16-Mar-2001
|
2001-03-15 23:27:13 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
|
|
|
|
|
|
A different PEP[1] suggests adding a builtin rational type to
|
|
|
|
|
Python. This PEP suggests changing the ddd.ddd float literal to a
|
|
|
|
|
rational in Python, and modifying non-integer division to return
|
|
|
|
|
it.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Rationale
|
|
|
|
|
|
2001-03-21 13:40:28 -05:00
|
|
|
|
Rational numbers are useful for exact and unsurprising arithmetic.
|
|
|
|
|
They give the correct results people have been taught in various
|
|
|
|
|
math classes. Making the "obvious" non-integer type one with more
|
2001-03-15 23:27:13 -05:00
|
|
|
|
predictable semantics will surprise new programmers less then
|
2001-03-21 13:40:28 -05:00
|
|
|
|
using floating point numbers. As quite a few posts on c.l.py and
|
|
|
|
|
on tutor@python.org have shown, people often get bit by strange
|
|
|
|
|
semantics of floating point numbers: for example, round(0.98, 2)
|
|
|
|
|
still gives 0.97999999999999998.
|
2001-03-15 23:27:13 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Proposal
|
|
|
|
|
|
|
|
|
|
Literals conforming to the regular expression '\d*.\d*' will be
|
|
|
|
|
rational numbers.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Backwards Compatibility
|
|
|
|
|
|
|
|
|
|
The only backwards compatible issue is the type of literals
|
|
|
|
|
mentioned above. The following migration is suggested:
|
|
|
|
|
|
|
|
|
|
1. "from __future__ import rational_literals" will cause all such
|
|
|
|
|
literals to be treated as rational numbers.
|
|
|
|
|
|
|
|
|
|
2. Python 2.2 will have a warning, turned off by default, about
|
|
|
|
|
such literals in the absence of a __future__ statement. The
|
|
|
|
|
warning message will contain information about the __future__
|
|
|
|
|
statement, and indicate that to get floating point literals,
|
|
|
|
|
they should be suffixed with "e0".
|
|
|
|
|
|
|
|
|
|
3. Python 2.3 will have the warning turned on by default. This
|
|
|
|
|
warning will stay in place for 24 months, at which time the
|
|
|
|
|
literals will be rationals and the warning will be removed.
|
|
|
|
|
|
|
|
|
|
|
2001-03-21 06:25:51 -05:00
|
|
|
|
Common Objections
|
|
|
|
|
|
|
|
|
|
Rationals are slow and memory intensive!
|
|
|
|
|
(Relax, I'm not taking floats away, I'm just adding two more characters.
|
|
|
|
|
1e0 will still be a float)
|
|
|
|
|
|
|
|
|
|
|
2001-03-15 23:27:13 -05:00
|
|
|
|
References
|
|
|
|
|
|
|
|
|
|
[1] PEP 239, Adding a Rational Type to Python, Zadka,
|
|
|
|
|
http://python.sourceforge.net/peps/pep-0239.html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Copyright
|
|
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Local Variables:
|
|
|
|
|
mode: indented-text
|
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
|
End:
|