[Python-ideas] numerical type combining integer and float/decimal properties [Was: Re: Python Numbers as Human Concept Decimal System]

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Thu Mar 13 18:28:49 CET 2014


Guido van Rossum <guido at ...> writes:

> 
> This representation makes more sense, it is fixed point. But you can just
use a single integer and keep track of where the point should be.

One of the reasons I started out with two separately stored parts has to do
with my initial motivation for forking the parallel Python Numbers as Human
Concept Decimal System discussion.

That one is about a new numeric 'd' literal and the discussion now seems to
go in the direction of having this generate a new number type closely
related to the current decimal.Decimal, but somewhat simplified.

I definitely agree with Oscar Benjamin on his point that decimal.Decimal
from the stdlib is too complex (especially with Contexts) to be made
accessible through a literal constructor, but I would even go a step further
than him and say that if such a new number type gets introduced it should
have additional properties that decimal.Decimal doesn't have.

One of the concerns about a new type seems to be that it makes Python's
number system even more complex rather than simplifying it, but my
suggestion would be to make the new literal create a compound number type
behaving like my PyNumber class.

Please regard my use of two integers as an implementation detail here. My
preference for a built-in type would actually be an integer for the integral
part, but a decimal for the fractional part, it was just a bit harder to
code up quickly.
I envision this decimal part to be essentially the simplified decimal that
Oscar Benjamin advocates for, so it would have most of the power of
decimal.Decimal, but it would only be concerned with the fractional part of
numbers.

The user would never directly encounter this new decimal type, but instead
work with the compound "PyNumber" type.
I agree, this still introduces a new type, but, importantly, this new type
could be considered a superset of int. In fact, every int could be thought
of as a "PyNumber" with a fractional part of 0 (just like real numbers could
be thought of as complex with an imaginary part of 0j).
In the long run, it would thus be possible to make, e.g., division of
integers return a "PyNumber" and to replace float in conversions from an
integral number to a fractional one. Then there would be no need anymore to
expose the skeleton of number representations by things like:

>>> float(math.factorial(500))
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    float(math.factorial(500))
OverflowError: long int too large to convert to float

and there would be no more precision loss like this occurring anymore:

>>> a = 26313083693369353016721801216
>>> a + .5
2.631308369336935e+28

So, essentially I would prefer a potential new number type to improve the
user-experience with Python numbers in more ways than currently discussed
with the 'd' literal - and to come back full circle now:

Having a separate representation for the type's integral component could be
advantageous when this is expected to be combined with ints a lot.

Cheers,
Wolfgang




More information about the Python-ideas mailing list