2001-03-15 23:24:17 -05:00
|
|
|
|
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
|
2001-03-16 11:02:24 -05:00
|
|
|
|
Post-History: 16-Mar-2001
|
2001-03-15 23:24:17 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2001-03-19 14:36:46 -05:00
|
|
|
|
.denominator. The language definition will promise that
|
2001-03-15 23:24:17 -05:00
|
|
|
|
|
|
|
|
|
r.denominator * r == r.numerator
|
|
|
|
|
|
2001-03-19 14:36:46 -05:00
|
|
|
|
that the GCD of the numerator and the denominator is 1 and that
|
|
|
|
|
the denominator is positive.
|
2001-03-15 23:24:17 -05:00
|
|
|
|
|
|
|
|
|
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,
|
2001-07-05 15:20:16 -04:00
|
|
|
|
http://www.python.org/peps/pep-0240.html
|
2001-03-15 23:24:17 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Copyright
|
|
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Local Variables:
|
|
|
|
|
mode: indented-text
|
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
|
End:
|