Movatterモバイル変換


[0]ホーム

URL:


Following system colour schemeSelected dark colour schemeSelected light colour scheme

Python Enhancement Proposals

PEP 240 – Adding a Rational Literal to Python

Author:
Christopher A. Craig <python-pep at ccraig.org>, Moshe Zadka <moshez at zadka.site.co.il>
Status:
Rejected
Type:
Standards Track
Created:
11-Mar-2001
Python-Version:
2.2
Post-History:
16-Mar-2001

Table of Contents

Abstract

Adifferent PEP suggests adding a builtin rational type toPython. This PEP suggests changing the ddd.ddd float literal to arational in Python, and modifying non-integer division to returnit.

BDFL Pronouncement

This PEP is rejected. The needs outlined in the rationale sectionhave been addressed to some extent by the acceptance ofPEP 327for decimal arithmetic. Guido also noted, “Rational arithmeticwas the default ‘exact’ arithmetic in ABC and it did not work out asexpected”. See the python-dev discussion on 17 June 2005[1].

Rationale

Rational numbers are useful for exact and unsurprising arithmetic.They give the correct results people have been taught in variousmath classes. Making the “obvious” non-integer type one with morepredictable semantics will surprise new programmers less thanusing floating point numbers. As quite a few posts on c.l.py andontutor@python.org have shown, people often get bit by strangesemantics of floating point numbers: for example,round(0.98,2)still gives 0.97999999999999998.

Proposal

Literals conforming to the regular expression'\d*.\d*' will berational numbers.

Backwards Compatibility

The only backwards compatible issue is the type of literalsmentioned above. The following migration is suggested:

  1. The next Python after approval will allowfrom__future__importrational_literalsto cause all such literals to be treated as rational numbers.
  2. Python 3.0 will have a warning, turned on by default, aboutsuch literals in the absence of a__future__ statement. Thewarning message will contain information about the__future__statement, and indicate that to get floating point literals,they should be suffixed with “e0”.
  3. Python 3.1 will have the warning turned off by default. Thiswarning will stay in place for 24 months, at which time theliterals will be rationals and the warning will be removed.

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)

Rationals must present themselves as a decimal float or they will behorrible for users expecting decimals (i.e.str(.5) should return'.5' andnot'1/2'). This means that many rationals must be truncated at somepoint, which gives us a new loss of precision.

References

[1]
Raymond Hettinger, Propose rejection of PEPs 239 and 240 – a builtinrational type and rational literalshttps://mail.python.org/pipermail/python-dev/2005-June/054281.html

Copyright

This document has been placed in the public domain.


Source:https://github.com/python/peps/blob/main/peps/pep-0240.rst

Last modified:2025-02-01 08:55:40 GMT


[8]ページ先頭

©2009-2025 Movatter.jp