rounding problem

Kristian M Zoerhoff zoerhoff at ukato.freeshell.org
Wed Feb 23 21:25:35 EST 2005


In article <gu6dnWNLB-FHuoDfRVn-tA at comcast.com>, Andy Leszczynski wrote:
> >>> a=1.1
> >>> a
> 1.1000000000000001
> >>>
> 
> 
> Is it normal?

Yes, for floating-point numbers. This is due to inherent imprecision
in how floats are represented in hardware. If you can live with being
a touch off that many decimal places out, you can just clean up upon
printing with something like:

>>>print "%0.1f" % (a)
1.1

for your value of a above. If you don't want to deal with this cruft,
you may want to give the Decimal class a gander:

>>>from Decimal import *
>>>setcontext(ExtendedContext)
>>>a = Decimal('1.1')
>>>a
'Decimal("1.1")'
>>> print a
1.1


-- 
zoerhoff(AT)sdf.lonestar.org 
kristian.zoerhoff(AT)gmail.com



More information about the Python-list mailing list