[Python-ideas] float vs decimal
William Dode
wilk at flibuste.net
Thu Mar 12 13:25:36 CET 2009
On 11-03-2009, Chris Rebert wrote:
[...]
> It's in the FAQ section of the decimal module -
> http://docs.python.org/library/decimal.html :
>
> 17. Is there a way to convert a regular float to a Decimal?
> A. Yes, all binary floating point numbers can be exactly expressed as
> a Decimal. An exact conversion may take more precision than intuition
> would suggest, so we trap Inexact to signal a need for more precision:
> def float_to_decimal(f):
> [definition snipped]
>
> 17. Why isn’t the float_to_decimal() routine included in the module?
> A. There is some question about whether it is advisable to mix binary
> and decimal floating point. Also, its use requires some care to avoid
> the representation issues associated with binary floating point:
>>>> float_to_decimal(1.1)
> Decimal('1.100000000000000088817841970012523233890533447265625')
I understand. It means that explicit is better and that we should know
what we do with decimal numbers...
thanks
--
William Dodé - http://flibuste.net
Informaticien Indépendant
More information about the Python-ideas
mailing list