On Sun, Dec 6, 2009 at 1:46 AM, Mark Dickinson <dicki...@gmail.com> wrote: > On Dec 5, 3:37 pm, Anton81 <gerenu...@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))
If you can depend on IEEE 754 semantics, one relatively robust method is to use the number of representable floats between two numbers. The main advantage compared to the proposed methods is that it somewhat automatically takes into account the amplitude of input numbers: abs(x - y) <= N * spacing(max(abs(x), abs(y))) Where spacing(a) is the smallest number such as a + spacing(a) != a. Whether a and b are small or big, the same value of N can be used, and it tells you how close two numbers are in terms of internal representation. Upcoming numpy 1.4.0 has an implementation for spacing - implementing your own for double is not difficult, though, cheers, David -- http://mail.python.org/mailman/listinfo/python-list