Python 2.3.7 has the following functions as number converters: float(num), int(num), long(num). But what happened to double(num)?
If my number is a=3.33333 and do round(_,2), the output I get is 3.3300000000000001. I was expecting to see 3.33.
What’s happening to here?
Thank you.
liliya

It gets weirder. Check this out

a = 3.333333
round(a,2)
# output> 3.3300000000000001
a = round(a,2)
a
# output> 3.3300000000000001
print a
# output> 3.33

What's up with that?

Thanks a bunch! I'm new to the language, trying to pick it up :)

This is a well know and well documented result on PCs due to the use of binary numbers. Our base 10 system also has problems (with numbers like 1/3 or Pi). The explaination at python.org is here www.python.org/doc/faq/general/#why-are-floating-point-calculations-so-inaccurate and http://www.lahey.com/float.htm A google for "floating point precision" or something similiar will yield more results. Most, if not all programming languages have add-ons for exact precision. In python it is the decimal module.

import decimal

x = decimal.Decimal("1")
y = decimal.Decimal("3")
print x / y

## set precision to 3
decimal.getcontext().prec = 3    

## set rounding type
decimal.getcontext().rounding=decimal.ROUND_HALF_EVEN
print x / y

I'm going to look for/get this decimal library and looking at the links you posted here right now. Thank you for the post, this helps a good deal.
l_w

I'm not finding Python 2.3 decimal module on the web. Does anybody know where I could downloaded it?

I'd like to have decimal module on 2.3.7 version, because the project I will work on in near future is developed in this version, other versions have compatibility problems.
So there is no decimal module for 2.3 anywhere?

I saw this link few days ago; unfortunately, it is a very old link and not a single link is valid on that page :(

This article has been dead for over six months. Start a new discussion instead.