PEP 238 (revised)

Rainer Deyke root at rainerdeyke.com
Sat Jul 28 21:22:31 EDT 2001


Here is an alternate numerical model:

'1.0' is a fixed point number with one digit to the right of the decimal
place.  '1.00' is a fixed point number with two digits to the right of the
decimal place.  '1e-1' is a fixed point number with one digit to the right
of the decimal point.  '1e1' is a fixed point number with negative one
digits to the right of the decimal point.  Conceptually, integers are fixed
point numbers with zero digits to the right of the decimal place.

Internally, each number could be represented as two integers (longs): one
'base' and 'exp', such that the value of the number is 'base + 10 ** exp'.
'-exp' is the number of digits to the right of the decimal point.

For addition (and subtraction), the number with fewer digits to the right of
the decimal point is padded with zeros.  Addition is therefore an exact
operation:

1.00 + 1.0 => 1.00 + 1.00 => 2.00

For multiplication, the resulting number has digits to the right of the
decimal point equal to the sum of the number of digits to the right of the
decimal point in its operands.  Multiplication is therefore an exact
operation:

0.1 * 0.1 => 0.01
1.00 * 1.0 => 1.000

There is no way to represent arbitrary ratios in this numerical model.
Division is therefore defined as a truncating operation (towards negative
infinity).  All multplication operations can be inverted with division, so
the number of digits to the right of the decimal point of 'a / b' is the
number of digits to the right of the decimal point of 'a' minus the number
of digits to the right of the decimal point of 'b':

0.01 / 0.1 => 0.1
1.000 / 1.0 => 1.00
5 / 2 => 2

This numerical model has several advantages over Python's current model.  It
has none of the dangers of floating points and rationals.  It is easy to
understand.  It works consistently on all computers without depending on the
underlying C library.  It unifies integer division with "real" division.
Its primary disadvantages are speed, incompatibility with third party
libraries, and its tie to the decimal system.

My goal here is not to replace Python's existing numerical model, or even to
add a new numerical model alomgside it.  If I really wanted this model, I
could easily implement it myself by creating a Python class.

I do have one question however: if I implement this numerical model and PEP
238 is accepted, which division operator should I overload for my division
operation (as defined above)?


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list