osdir.com


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

Question about floating point


"Frank Millman"  wrote in message news:...

"Frank Millman"  wrote in message news:pm3l2m$kv4$1 at blaine.gmane.org...
>
> I know about this gotcha -
>
> >>> x = 1.1 + 2.2
> >>> x
> 3.3000000000000003
>
[...]

I have enjoyed the discussion, and I have learnt a lot about floating point. 
Thanks to all.

I have just noticed one oddity which I thought worth a mention.

>>> 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.

Frank