PEP 239, Adding a Rational Type to Python, Moshe Zadka
[checking in for Moshe, after editorial, spell check, and formatting passes by Barry]
This commit is contained in:
parent
f7b42c0a8e
commit
c671a7371d
|
@ -0,0 +1,78 @@
|
|||
PEP: 239
|
||||
Title: Adding a Rational Type 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
|
||||
Post-History:
|
||||
|
||||
|
||||
Abstract
|
||||
|
||||
Python has no numeric type with the semantics of an unboundedly
|
||||
precise rational number. This proposal explains the semantics of
|
||||
such a type, and suggests builtin functions and literals to
|
||||
support such a type. This PEP suggests no literals for rational
|
||||
numbers; that is left for another PEP[1].
|
||||
|
||||
|
||||
Rationale
|
||||
|
||||
While sometimes slower and more memory intensive (in general,
|
||||
unboundedly so) rational arithmetic captures more closely the
|
||||
mathematical ideal of numbers, and tends to have behavior which is
|
||||
less surprising to newbies. Though many Python implementations of
|
||||
rational numbers have been written, none of these exist in the
|
||||
core, or are documented in any way. This has made them much less
|
||||
accessible to people who are less Python-savvy.
|
||||
|
||||
|
||||
RationalType
|
||||
|
||||
There will be a new numeric type added called RationalType. Its
|
||||
unary operators will do the obvious thing. Binary operators will
|
||||
coerce integers and long integers to rationals, and rationals to
|
||||
floats and complexes.
|
||||
|
||||
The following attributes will be supported: .numerator and
|
||||
.denominator. The language definition will not define these other
|
||||
then that:
|
||||
|
||||
r.denominator * r == r.numerator
|
||||
|
||||
In particular, no guarantees are made regarding the GCD or the
|
||||
sign of the denominator, even though in the proposed
|
||||
implementation, the GCD is always 1 and the denominator is always
|
||||
positive.
|
||||
|
||||
The method r.trim(max_denominator) will return the closest
|
||||
rational s to r such that abs(s.denominator) <= max_denominator.
|
||||
|
||||
|
||||
The rational() Builtin
|
||||
|
||||
This function will have the signature rational(n, d=1). n and d
|
||||
must both be integers, long integers or rationals. A guarantee is
|
||||
made that
|
||||
|
||||
rational(n, d) * d == n
|
||||
|
||||
|
||||
References
|
||||
|
||||
[1] PEP 240, Adding a Rational Literal to Python, Zadka,
|
||||
http://python.sourceforge.net/peps/pep-0240.html
|
||||
|
||||
|
||||
Copyright
|
||||
|
||||
This document has been placed in the public domain.
|
||||
|
||||
|
||||
|
||||
Local Variables:
|
||||
mode: indented-text
|
||||
indent-tabs-mode: nil
|
||||
End:
|
Loading…
Reference in New Issue