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

Recommended Answers

All 13 Replies

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

why not go for Python 2.5 ?

why not go for Python 2.5 ?

Yes, decimal was first included in Python 2.4 On linux, upgrading is as simple as telling the package manager to upgrade. For MS Windows, http://python.org/download/releases/2.5.4/ ActiveState also has a version. Either version is fine. Also, you want to update when new releases come out for security reasons.

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?

This is the first hit for a Google

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

Thank you for the url, I'll try it out!

that worked! thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.