Why not FP for Money?

Carlos Ribeiro carribeiro at gmail.com
Thu Sep 23 15:27:29 EDT 2004


On 23 Sep 2004 15:08:53 -0400, Aahz <aahz at pythoncraft.com> wrote:
> Not sure of precise syntax (haven't tried 2.4 yet), but this should
> work:
> 
>     a = decimal.from_float(35.72)

I have downloaded 2.4, but couldn't install it yet. But the release
notes states that, after long discussions, support for
Decimal.from_float() was removed from the release, due to people being
unable to reach a consensus on the precision to be used in the
conversion.

The problem is (to put it really simply) that 35.72 isn't really 35.72, it is:

32.719999999999999

What should from_float do? Truncate it to 2 digits isn't possible --
the fact that the literal was written with two digits was lost in the
innards of the lexical analyzer long before from_float() is called.
And to represent it internally as 32.719999999999999 will cause
surprises for most people; but to truncate it arbitrarily is also
problematic. So it was removed (that's what the release notes states,
anyway)

A native Money type, either with the $ signal ($35.72) or the suffix
(35.72D) would not present the same issues, because the literal would
be converted directly to the fixed point representation without the
float intermediate step.

-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list