[Python-Dev] Kinds
Paul F. Dubois
paul@pfdubois.com
Tue, 13 Mar 2001 08:38:35 -0800
This is a multi-part message in MIME format.
------=_NextPart_000_0008_01C0AB98.FE86CE00
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
I was asked to write down what I said at the dev day session about kinds. I
have put this in the form of a proposal-like writeup which is attached. I
hope this helps you undestand what I meant.
------=_NextPart_000_0008_01C0AB98.FE86CE00
Content-Type: text/plain;
name="kinds.txt"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="kinds.txt"
A proposal on kinds for Python
This proposal aims to give the user optional control over the precision =
and range of numeric computations so that a computation can be written =
once and run anywhere with at least the desired precision and range.
1. Each Python compiler may define as many "kinds" of integer and =
floating point numbers as it likes,
except that it must propose at least one kind of integer corresponding =
to the existing int, and must propose at least one kind of floating =
point number, equivalent to the present float. These two required kinds =
are called the default integer kind and the default float kind. The =
range and precision of the default kinds are processor dependent, as at =
present.
Each kind other than the default is given a processor-dependent small =
integer label called the kind number.
1. The builtin functions int and float are given an optional second =
argument with default value None.
int(x, kind=3DNone)
float(x, kind=3DNone)
If kind is None, these attempt to convert x to the default kind of =
that type. If the kind is a small integer, the processor uses a =
conversion routine to convert x to that kind, if that kind number is =
defined on this processor. Otherwise,
an exception is generated. Floating point numbers are truncated to less =
precision if necessary but if they do not fit in the target's dynamic =
range an exception is thrown.
2. Two new builtin functions are defined. They return kind numbers.
selected_int_kind (n)
-- return the number of a kind that will hold an integer number in =
the range -10**n to 10**n.
selected_float_kind (nd, n)
-- return the number of a kind that will hold a floating-point =
number with at least nd digits of precision and
at least a dynamic range of 10**+/- n
If no kind with the desired qualities exists an exception is thrown.
3. Modification to the literal parser ala Fortran 90.
An integer or floating point literal may be followed by _name, where =
name is a legal identifier. For example,
1.23e10_precise or 222_big. This is syntactic sugar for float(x, =
name) or int(x, name) respectively.
Example:
single =3D selected_float_kind(6, 90)
double =3D selected_float_kind(15, 300)
x =3D 1.e100_double
y =3D 1.e20_single
z =3D 1.2
w =3D x * float(z, double)
u =3D float(x, single) * y
=20
Open questions: specify exactly what exception is thrown in each case, =
and whether or not there is a standard kind number=20
or name for the existing long.
------=_NextPart_000_0008_01C0AB98.FE86CE00--