Comparing floats

Arnaud Delobelle arnodel at gmail.com
Sat Nov 27 19:09:59 EST 2010


Terry Reedy <tjreedy at udel.edu> 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 ==
0.

-- 
Arnaud




More information about the Python-list mailing list