Question about floating point

Frank Millman frank at chagford.com
Sat Sep 1 07:27:59 EDT 2018


"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









More information about the Python-list mailing list