From: Anton81 on
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?
From: Mark Dickinson on
On Dec 5, 3:37 pm, 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?

Well, it depends a lot on the details of the application, but
a good general scheme is to allow both a fixed relative error
and an absolute error, and to assert that your two values are
'nearly equal' if they're within *either* the relative error *or*
the absolute error. Something like, for example:

def almostEqual(expected, actual, rel_err=1e-7, abs_err = 1e-20):
absolute_error = abs(actual-expected)
return absolute_error <= max(abs_err, rel_err * abs(expected))

Then choose the values of rel_err and abs_err to suit your
application.

What sort of calculations are you doing?
--
Mark
From: Raymond Hettinger on
On Dec 5, 7:37 am, 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?

Short answer: use round().
Less short answer: use Decimal with a high precision and then round()
or quantize.
Long answer: the amount of error depends on the calculation and the
scale of the inputs; some calculations potentially propagate tiny
errors to the point where they become large enough to overpower the
signal in your data (e.g. the Lorentz equation or some other chaotic
sequence).


Raymond
From: Mark Dickinson on
On Dec 5, 8:25 pm, Raymond Hettinger <pyt...(a)rcn.com> wrote:
> On Dec 5, 7:37 am, 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?
>
> Short answer: use round().

Can you explain how this would work? I'm imagining a test
something like:

if round(x, 6) == round(y, 6): ...

but that still would end up missing some cases where x and y
are equal to within 1ulp, which presumably isn't what's wanted:

>>> x, y = 0.1234565, 0.123456500000000004
>>> round(x, 6) == round(y, 6)
False

--
Mark
From: Raymond Hettinger on
On Dec 5, 12:56 pm, Mark Dickinson <dicki...(a)gmail.com> wrote:
> On Dec 5, 8:25 pm, Raymond Hettinger <pyt...(a)rcn.com> wrote:
>
> > On Dec 5, 7:37 am, 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?
>
> > Short answer: use round().
>
> Can you explain how this would work?  I'm imagining a test
> something like:
>
> if round(x, 6) == round(y, 6): ...
>
> but that still would end up missing some cases where x and y
> are equal to within 1ulp, which presumably isn't what's wanted:

if not round(x - y, 6): ...


Raymond