[Tutor] Float Numbers. 98/100 returns 0.97999999999999998

Jethro Cramp jethro@163bj.com
Wed, 21 Mar 2001 11:52:40 +0800


I have written a small number of functions for converting measurements from
one unit to another for example from centimetres to metres. An example of
this function is:

def cmTOm(length):
	return length/100

When I tested this with cmTOm(98) I found that I had fallen into the first
trap of using numbers in python when I was returned 0. I checked and
realised that this was normal. My understanding is that I should change the
length to a float first so I used:

def cmTOm(length):
	return float(length)/100

When I tested this with cmTOm(98) I was surprised to get the answer:

	0.97999999999999998

Although pretty accurate this is not the correct answer and will certainly
cause minor unacceptable errors to creep into my application. I thought that
if I rounded the digit to 2 decimal places this should solve the problem.
But using the function round(number, 2) gives me the result:

	0.97999999999999998

What should I be doing to get the correct result of 0.98?

I am using Python 2.0 on WindowsME.

Thanks in advance,

Jethro