[Python-ideas] Python Float Update

M.-A. Lemburg mal at egenix.com
Tue Jun 2 10:19:39 CEST 2015

On 02.06.2015 03:37, Steven D'Aprano wrote:
> On Mon, Jun 01, 2015 at 05:52:35PM +0300, Joonas Liik wrote:
>> Having some sort of decimal literal would have some advantages of its own,
>> for one it could help against this sillyness:
>>>>> Decimal(1.3)
>> Decimal('1.3000000000000000444089209850062616169452667236328125')
> Why is that silly? That's the actual value of the binary float 1.3 
> converted into base 10. If you want 1.3 exactly, you can do this:
>>>>> Decimal('1.3')
>> Decimal('1.3')
> Is that really so hard for people to learn? 

Joonas, I think you're approaching this from the wrong angle.

People who want to get an exact decimal from a literal, will
use the string representation to define it, not a float

In practice, you typically read the data from some file or stream
anyway, so it already comes as string value and if you want to
convert an actual float to a decimal, this will most likely
not be done in a literal way, but instead by passed in to
the Decimal constructor as variable, so there's no literal

It may be good to provide some alternative ways of converting
a float to a decimal, e.g. one which uses the float repr logic
to overcome things like repr(float(1.1)) == '1.1000000000000001'
instead of a direct conversion:

>>> Decimal(1.1)
>>> Decimal(repr(1.1))

These could be added as parameter to the Decimal constructor.

Marc-Andre Lemburg

Professional Python Services directly from the Source  (#1, Jun 02 2015)
>>> Python Projects, Coaching and Consulting ...  http://www.egenix.com/
>>> mxODBC Plone/Zope Database Adapter ...       http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/

::::: Try our mxODBC.Connect Python Database Interface for free ! ::::::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611

More information about the Python-ideas mailing list