2000-11-05 15:36:06 -05:00
|
|
|
|
PEP: 228
|
2000-11-05 11:55:24 -05:00
|
|
|
|
Title: Reworking Python's Numeric Model
|
|
|
|
|
Version: $Revision$
|
2001-07-25 12:52:00 -04:00
|
|
|
|
Author: pep@zadka.site.co.il (Moshe Zadka), guido@python.org (Guido van Rossum)
|
2000-11-05 11:55:24 -05:00
|
|
|
|
Status: Draft
|
|
|
|
|
Type: Standards Track
|
2000-11-06 10:29:58 -05:00
|
|
|
|
Python-Version: ??
|
2000-11-05 11:55:24 -05:00
|
|
|
|
Created: 4-Nov-2000
|
|
|
|
|
Post-History:
|
|
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
|
|
2000-11-06 10:29:58 -05:00
|
|
|
|
Today, Python's numerical model is similar to the C numeric model:
|
|
|
|
|
there are several unrelated numerical types, and when operations
|
|
|
|
|
between numerical types are requested, coercions happen. While
|
|
|
|
|
the C rationale for the numerical model is that it is very similar
|
2001-02-27 16:01:22 -05:00
|
|
|
|
to what happens at the hardware level, that rationale does not
|
2000-11-06 10:29:58 -05:00
|
|
|
|
apply to Python. So, while it is acceptable to C programmers that
|
2001-07-26 12:49:34 -04:00
|
|
|
|
2/3 == 0, it is surprising to many Python programmers.
|
2000-11-06 10:29:58 -05:00
|
|
|
|
|
2001-07-25 12:53:19 -04:00
|
|
|
|
NOTE: in the light of recent discussions in the newsgroup, the
|
2001-07-26 12:49:34 -04:00
|
|
|
|
motivation in this PEP (and details) need to be extended.
|
2001-07-25 12:53:19 -04:00
|
|
|
|
|
2000-11-05 11:55:24 -05:00
|
|
|
|
|
|
|
|
|
Rationale
|
|
|
|
|
|
2001-02-27 16:01:22 -05:00
|
|
|
|
In usability studies, one of the least usable aspect of Python was
|
2000-11-06 10:29:58 -05:00
|
|
|
|
the fact that integer division returns the floor of the division.
|
|
|
|
|
This makes it hard to program correctly, requiring casts to
|
|
|
|
|
float() in various parts through the code. Python's numerical
|
2001-02-27 16:01:22 -05:00
|
|
|
|
model stems from C, while an model that might be easier to work with
|
|
|
|
|
can be based on the mathematical understanding of numbers.
|
2000-11-06 10:29:58 -05:00
|
|
|
|
|
2000-11-05 11:55:24 -05:00
|
|
|
|
|
|
|
|
|
Other Numerical Models
|
|
|
|
|
|
2000-11-06 10:29:58 -05:00
|
|
|
|
Perl's numerical model is that there is one type of numbers --
|
|
|
|
|
floating point numbers. While it is consistent and superficially
|
|
|
|
|
non-surprising, it tends to have subtle gotchas. One of these is
|
|
|
|
|
that printing numbers is very tricky, and requires correct
|
|
|
|
|
rounding. In Perl, there is also a mode where all numbers are
|
|
|
|
|
integers. This mode also has its share of problems, which arise
|
|
|
|
|
from the fact that there is not even an approximate way of
|
|
|
|
|
dividing numbers and getting meaningful answers.
|
2000-11-05 11:55:24 -05:00
|
|
|
|
|
|
|
|
|
|
2000-11-06 10:29:58 -05:00
|
|
|
|
Suggested Interface For Python's Numerical Model
|
|
|
|
|
|
|
|
|
|
While coercion rules will remain for add-on types and classes, the
|
|
|
|
|
built in type system will have exactly one Python type -- a
|
|
|
|
|
number. There are several things which can be considered "number
|
|
|
|
|
methods":
|
2000-11-05 11:55:24 -05:00
|
|
|
|
|
|
|
|
|
1. isnatural()
|
|
|
|
|
2. isintegral()
|
|
|
|
|
3. isrational()
|
|
|
|
|
4. isreal()
|
|
|
|
|
5. iscomplex()
|
|
|
|
|
|
|
|
|
|
a. isexact()
|
|
|
|
|
|
2001-02-27 16:01:22 -05:00
|
|
|
|
Obviously, a number which answers true to a question from 1 to 5, will
|
|
|
|
|
also answer true to any following question. If "isexact()" is not true,
|
|
|
|
|
then any answer might be wrong.
|
2000-11-06 10:29:58 -05:00
|
|
|
|
(But not horribly wrong: it's close to the truth.)
|
2000-11-05 11:55:24 -05:00
|
|
|
|
|
|
|
|
|
Now, there is two thing the models promises for the field operations
|
|
|
|
|
(+, -, /, *):
|
|
|
|
|
|
2000-11-06 10:29:58 -05:00
|
|
|
|
- If both operands satisfy isexact(), the result satisfies
|
|
|
|
|
isexact().
|
2000-11-05 11:55:24 -05:00
|
|
|
|
|
2000-11-06 10:29:58 -05:00
|
|
|
|
- All field rules are true, except that for not-isexact() numbers,
|
|
|
|
|
they might be only approximately true.
|
2000-11-05 11:55:24 -05:00
|
|
|
|
|
2001-02-27 16:01:22 -05:00
|
|
|
|
One consequence of these two rules is that all exact calcutions
|
|
|
|
|
are done as (complex) rationals: since the field laws must hold,
|
|
|
|
|
then
|
|
|
|
|
|
|
|
|
|
(a/b)*b == a
|
|
|
|
|
|
|
|
|
|
must hold.
|
|
|
|
|
|
|
|
|
|
There is built-in function, inexact() which takes a number
|
2000-11-05 11:55:24 -05:00
|
|
|
|
and returns an inexact number which is a good approximation.
|
2001-02-27 16:01:22 -05:00
|
|
|
|
Inexact numbers must be as least as accurate as if they were
|
|
|
|
|
using IEEE-754.
|
|
|
|
|
|
|
|
|
|
Several of the classical Python functions will return exact numbers
|
|
|
|
|
even when given inexact numbers: e.g, int().
|
2000-11-05 11:55:24 -05:00
|
|
|
|
|
2001-02-27 16:01:22 -05:00
|
|
|
|
Coercion
|
2000-11-05 11:55:24 -05:00
|
|
|
|
|
2001-02-27 16:01:22 -05:00
|
|
|
|
The number type does not define nb_coerce
|
|
|
|
|
Any numeric operation slot, when receiving something other then PyNumber,
|
|
|
|
|
refuses to implement it.
|
2000-11-06 10:29:58 -05:00
|
|
|
|
|
2000-11-05 11:55:24 -05:00
|
|
|
|
Inexact Operations
|
|
|
|
|
|
2000-11-06 10:29:58 -05:00
|
|
|
|
The functions in the "math" module will be allowed to return
|
|
|
|
|
inexact results for exact values. However, they will never return
|
2001-02-27 16:01:22 -05:00
|
|
|
|
a non-real number. The functions in the "cmath" module are also
|
|
|
|
|
allowed to return an inexact result for an exact argument, and are
|
|
|
|
|
furthermore allowed to return a complex result for a real
|
|
|
|
|
argument.
|
2000-11-06 10:29:58 -05:00
|
|
|
|
|
2000-11-05 11:55:24 -05:00
|
|
|
|
|
2000-11-05 15:36:06 -05:00
|
|
|
|
Numerical Python Issues
|
2000-11-05 11:55:24 -05:00
|
|
|
|
|
2000-11-06 10:29:58 -05:00
|
|
|
|
People who use Numerical Python do so for high-performance vector
|
|
|
|
|
operations. Therefore, NumPy should keep its hardware based
|
|
|
|
|
numeric model.
|
|
|
|
|
|
2000-11-05 11:55:24 -05:00
|
|
|
|
|
2000-11-05 15:36:06 -05:00
|
|
|
|
Unresolved Issues
|
2000-11-05 11:55:24 -05:00
|
|
|
|
|
2000-11-05 15:36:06 -05:00
|
|
|
|
Which number literals will be exact, and which inexact?
|
2000-11-05 11:55:24 -05:00
|
|
|
|
|
2001-02-27 16:01:22 -05:00
|
|
|
|
How do we deal with IEEE 754 operations? (probably, isnan/isinf should
|
|
|
|
|
be methods)
|
2000-11-05 11:55:24 -05:00
|
|
|
|
|
2001-07-26 12:49:34 -04:00
|
|
|
|
On 64-bit machines, comparisons between ints and floats may be
|
|
|
|
|
broken when the comparison involves conversion to float. Ditto
|
|
|
|
|
for comparisons between longs and floats. This can be dealt with
|
|
|
|
|
by avoiding the conversion to float. (Due to Andrew Koenig.)
|
|
|
|
|
|
2000-11-06 10:29:58 -05:00
|
|
|
|
|
2000-11-05 11:55:24 -05:00
|
|
|
|
Copyright
|
|
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|
|
|
|
|
|
2000-11-06 10:29:58 -05:00
|
|
|
|
|
2000-11-05 11:55:24 -05:00
|
|
|
|
|
|
|
|
|
Local Variables:
|
|
|
|
|
mode: indented-text
|
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
|
End:
|