osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Question about floating point


On Sat, 01 Sep 2018 13:27:59 +0200, Frank Millman wrote:

>>>> from decimal import Decimal as D
>>>> f"{D('1.1')+D('2.2'):.60f}"
> '3.300000000000000000000000000000000000000000000000000000000000'
>>>> '{:.60f}'.format(D('1.1') + D('2.2'))
> '3.300000000000000000000000000000000000000000000000000000000000'
>>>> '%.60f' % (D('1.1') + D('2.2'))
> '3.299999999999999822364316059974953532218933105468750000000000'
>>>>
>>>>
> The first two format methods behave as expected. The old-style '%'
> operator does not.

The % operator casts the argument to a (binary) float. The other two 
don't need to, because they call Decimal's own format method.


-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson