Comparing floats

Arnaud Delobelle arnodel at
Sun Nov 28 01:09:59 CET 2010

Terry Reedy <tjreedy at> writes:

> 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

That'll throw an exception if i == -j.  You could replace (i+j) with
math.hypot(i, j) or abs(i)+abs(j) but it will still fail when i == j ==


More information about the Python-list mailing list