2001-03-15 23:27:13 -05:00
|
|
|
PEP: 240
|
|
|
|
Title: Adding a Rational Literal to Python
|
2017-01-10 01:52:57 -05:00
|
|
|
Author: Christopher A. Craig <python-pep@ccraig.org>, Moshe Zadka <moshez@zadka.site.co.il>
|
2005-06-17 17:38:02 -04:00
|
|
|
Status: Rejected
|
2001-03-15 23:27:13 -05:00
|
|
|
Type: Standards Track
|
2017-01-10 01:52:57 -05:00
|
|
|
Content-Type: text/x-rst
|
2001-03-15 23:27:13 -05:00
|
|
|
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
|
2017-01-10 01:52:57 -05:00
|
|
|
========
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
A :pep:`different PEP <239>` suggests adding a builtin rational type to
|
2017-01-10 01:52:57 -05:00
|
|
|
Python. This PEP suggests changing the ddd.ddd float literal to a
|
|
|
|
rational in Python, and modifying non-integer division to return
|
|
|
|
it.
|
2001-03-15 23:27:13 -05:00
|
|
|
|
|
|
|
|
2005-06-17 17:38:02 -04:00
|
|
|
BDFL Pronouncement
|
2017-01-10 01:52:57 -05:00
|
|
|
==================
|
|
|
|
|
|
|
|
This PEP is rejected. The needs outlined in the rationale section
|
2022-01-21 06:03:51 -05:00
|
|
|
have been addressed to some extent by the acceptance of :pep:`327`
|
2017-01-10 01:52:57 -05:00
|
|
|
for decimal arithmetic. Guido also noted, "Rational arithmetic
|
|
|
|
was the default 'exact' arithmetic in ABC and it did not work out as
|
2022-01-21 06:03:51 -05:00
|
|
|
expected". See the python-dev discussion on 17 June 2005 [1]_.
|
2005-06-17 17:38:02 -04:00
|
|
|
|
2001-03-15 23:27:13 -05:00
|
|
|
|
|
|
|
Rationale
|
2017-01-10 01:52:57 -05:00
|
|
|
=========
|
2001-03-15 23:27:13 -05:00
|
|
|
|
2017-01-10 01:52:57 -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
|
|
|
|
predictable semantics will surprise new programmers less than
|
|
|
|
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
|
2017-04-05 12:14:26 -04:00
|
|
|
semantics of floating point numbers: for example, ``round(0.98, 2)``
|
2017-01-10 01:52:57 -05:00
|
|
|
still gives 0.97999999999999998.
|
2001-03-15 23:27:13 -05:00
|
|
|
|
|
|
|
|
|
|
|
Proposal
|
2017-01-10 01:52:57 -05:00
|
|
|
========
|
2001-03-15 23:27:13 -05:00
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
Literals conforming to the regular expression ``'\d*.\d*'`` will be
|
2017-01-10 01:52:57 -05:00
|
|
|
rational numbers.
|
2001-03-15 23:27:13 -05:00
|
|
|
|
|
|
|
|
|
|
|
Backwards Compatibility
|
2017-01-10 01:52:57 -05:00
|
|
|
=======================
|
2001-03-15 23:27:13 -05:00
|
|
|
|
2017-01-10 01:52:57 -05:00
|
|
|
The only backwards compatible issue is the type of literals
|
|
|
|
mentioned above. The following migration is suggested:
|
2001-03-15 23:27:13 -05:00
|
|
|
|
2017-01-10 01:52:57 -05:00
|
|
|
1. The next Python after approval will allow
|
|
|
|
``from __future__ import rational_literals``
|
|
|
|
to cause all such literals to be treated as rational numbers.
|
2001-03-15 23:27:13 -05:00
|
|
|
|
2017-01-10 01:52:57 -05:00
|
|
|
2. Python 3.0 will have a warning, turned on by default, about
|
2022-10-14 01:01:49 -04:00
|
|
|
such literals in the absence of a ``__future__`` statement. The
|
2017-01-10 01:52:57 -05:00
|
|
|
warning message will contain information about the ``__future__``
|
|
|
|
statement, and indicate that to get floating point literals,
|
|
|
|
they should be suffixed with "e0".
|
2001-03-15 23:27:13 -05:00
|
|
|
|
2017-01-10 01:52:57 -05:00
|
|
|
3. Python 3.1 will have the warning turned off 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-15 23:27:13 -05:00
|
|
|
|
|
|
|
|
2001-03-21 06:25:51 -05:00
|
|
|
Common Objections
|
2017-01-10 01:52:57 -05:00
|
|
|
=================
|
2001-03-21 06:25:51 -05:00
|
|
|
|
2017-01-10 01:52:57 -05:00
|
|
|
Rationals are slow and memory intensive!
|
|
|
|
(Relax, I'm not taking floats away, I'm just adding two more characters.
|
2017-04-05 12:14:26 -04:00
|
|
|
``1e0`` will still be a float)
|
2017-01-10 01:52:57 -05:00
|
|
|
|
|
|
|
Rationals must present themselves as a decimal float or they will be
|
2017-04-05 12:14:26 -04:00
|
|
|
horrible for users expecting decimals (i.e. ``str(.5)`` should return ``'.5'`` and
|
|
|
|
not ``'1/2'``). This means that many rationals must be truncated at some
|
2017-01-10 01:52:57 -05:00
|
|
|
point, which gives us a new loss of precision.
|
2001-03-21 06:25:51 -05:00
|
|
|
|
2003-01-17 19:59:04 -05:00
|
|
|
|
2001-03-21 06:25:51 -05:00
|
|
|
|
2001-03-15 23:27:13 -05:00
|
|
|
References
|
2017-01-10 01:52:57 -05:00
|
|
|
==========
|
2001-03-15 23:27:13 -05:00
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
.. [1] Raymond Hettinger, Propose rejection of PEPs 239 and 240 -- a builtin
|
2017-01-10 01:52:57 -05:00
|
|
|
rational type and rational literals
|
|
|
|
https://mail.python.org/pipermail/python-dev/2005-June/054281.html
|
2001-03-15 23:27:13 -05:00
|
|
|
|
|
|
|
|
|
|
|
Copyright
|
2017-01-10 01:52:57 -05:00
|
|
|
=========
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|