From: sturlamolden on
On 5 Des, 16:37, Anton81 <gerenu...(a)googlemail.com> wrote:
> I'd like to do calculations with floats and at some point equality of
> two number will be checked.
> What is the best way to make sure that equality of floats will be
> detected, where I assume that mismatches beyond a certain point are
> due to truncation errors?


isequal = lambda x,y : abs(x-y) < eps

where eps is the truncation error.



From: Tim Roberts on
Raymond Hettinger <python(a)rcn.com> wrote:
>
> if not round(x - y, 6): ...

That's a dangerous suggestion. It only works if x and y happen to be
roughly in the range of integers.

For example, here x and y are within roundoff error of each other, but
round doesn't know it:
>>> x=1e32
>>> y=x+1e16
>>> x-y
-18014398509481984.0
>>> round(x-y,6)
-18014398509481984.0

It fails in the other direction when the numbers are small:
>>> x=.0000000123
>>> y=.0000000234
>>> x-y
-1.1100000000000002e-008
>>> round(x-y,6)
0.0

Mark's solution is the generically correct one, which takes into account
the rough range of the values.
--
Tim Roberts, timr(a)probo.com
Providenza & Boekelheide, Inc.
From: Raymond Hettinger on
On Dec 5, 11:42 pm, Tim Roberts <t...(a)probo.com> wrote:
> Raymond Hettinger <pyt...(a)rcn.com> wrote:
>
> >   if not round(x - y, 6): ...
>
> That's a dangerous suggestion.  It only works if x and y happen to be
> roughly in the range of integers.

Right. Using abs(x-y) < eps is the way to go.


Raymond
From: dbd on
On Dec 6, 1:12 am, Raymond Hettinger <pyt...(a)rcn.com> wrote:
> On Dec 5, 11:42 pm, Tim Roberts <t...(a)probo.com> wrote:
>
> > Raymond Hettinger <pyt...(a)rcn.com> wrote:
>
> > >   if not round(x - y, 6): ...
>
> > That's a dangerous suggestion.  It only works if x and y happen to be
> > roughly in the range of integers.
..>
..> Right.  Using abs(x-y) < eps is the way to go.
..>
..> Raymond

This only works when abs(x) and abs(y) are larger that eps, but not
too much larger.

Mark's suggestion is longer, but it works. The downside is it requires
you to think about the scale and accuracy of your application.

Dale B. Dalrymple
From: Anton81 on
I do some linear algebra and whenever the prefactor of a vector turns
out to be zero, I want to remove it.

I'd like to keep the system comfortable. So basically I should write a
new class for numbers that has it's own __eq__ operator?
Is there an existing module for that?