[Python-ideas] Python Float Update
Alexander Walters
tritium-list at sdamon.com
Tue Jun 2 11:38:47 CEST 2015
I think there is another discussion to have here, and that is making
Decimal part of the language (__builtin(s)__) vs. part of the library
(which implementations can freely omit). If it were part of the
language, then maybe, just maybe, a literal syntax should be considered.
As it stands, Decimal and Fraction are libraries - implementations of
python are free to omit them (as I think some of the embedded platform
implementations do), and it currently does not make a lick of sense to
add syntax for something that is only in the library.
On 6/2/2015 04:19, M.-A. Lemburg wrote:
> 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
> representation.
>
> 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
> involved.
>
> 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('1.100000000000000088817841970012523233890533447265625')
>>>> Decimal(repr(1.1))
> Decimal('1.1')
>
> These could be added as parameter to the Decimal constructor.
>
More information about the Python-ideas
mailing list