Comparing floats

Terry Reedy tjreedy at
Sun Nov 28 00:23:48 CET 2010

On 11/27/2010 5:55 PM, kj wrote:

> Therefore, to implement this multiplication operation I need to
> have a way to verify that the float tuples C and D are "equal".

I might try the average relative difference:
sum(abs((i-j)/(i+j)) for i,j in zip(C,D))/n # assuming lengths constant

> Certainly, I want to do this in in a way that takes into account
> the fact machine computations with floats can produce small
> differences between numbers that are notionally the same.

The problem is that the appropriate value may depend on the application 
and the source of the floats. If they are measured to 3 decimal places, 
you need a large value. If they are calculated, you need much smaller.

Terry Jan Reedy

More information about the Python-list mailing list