double arithmetic precession and equality
Hi, I was wondering if boost provides any support for testing equality of two doubles. In principle in C/C++ you cannot rely on equality because it uses bitwise comparison of doubles and in arithmetic expressions results may have lost some precession. An example will help: #include <iostream> int main(void) { double start = 3.123; double end = 4.124; double diff = end - start; // == 1.001 ??? if (diff != 1.001) { std::cout << diff << " and " << 1.001 << " are not equal" << std::endl; } } On my machine, this example outputs: 1.001 and 1.001 are not equal I am using a lot of arithmetic expressions on floating-point values and also need to test for equality. Also, at compile-time I do not know the precession so comparing within the range of an epsilon seems impossible. So does boost have anything for this I don't know of? Or any other libraries? Thanks, Andrej ___________________________________________________________ Yahoo! Answers - Got a question? Someone out there knows the answer. Try it now. http://uk.answers.yahoo.com/
Andrej van der Zee wrote:
Hi,
I was wondering if boost provides any support for testing equality of two doubles.
You could take a look at the "close_at_tolerance" class in boost/test/floating_point_comparison.hpp, but I'm not sure it's all that easy to use: the tolerance has to be specified in percentage points. The current SVN trunk (and Boost-1.35 when it's released) also has this function: http://svn.boost.org/svn/boost/trunk/libs/math/doc/sf_and_dist/html/math_too... for computing the relative difference between two floating point values.
I am using a lot of arithmetic expressions on floating-point values and also need to test for equality. Also, at compile-time I do not know the precession so comparing within the range of an epsilon seems impossible.
Well you're going to have to define an acceptable tolerance for the definition of "equal", as no library can do that for you. What do you need this for? Testing for equality in mathematical routines is IMO extremely rare, if you're testing the result of calculations then you might want to take a look at how the special functions test cases handle this in the Boost.Math library (again current Trunk and next release only). HTH, John.
So does boost have anything for this I don't know of? Or any other libraries?
Yeah, I think the question is, " what are you really trying to do?" Even with Java's repeatable-to-the-last-bit floating point spec, this would have only mattered to me once in writing an arithmetic encoder as , in this example, debug decoders would not work with release encoders. Then, I guess the questions would be, " what other abstractions would better address my objectives?" Close is supposed to be good enough for floating point and I wouldn't even rely on a given test branching reproducibly. For example, let's say you are trying to follow a signal and have a locked and unlocked state and look at behaviour as you try to obtain lock. Presumably, the output would vary a little if you chose unlocked for an extra cycle or two but who would care? You really have to look at floating point as not-so-deterministic. If you are tracking money or something, ints or strings would be better.
Date: Wed, 19 Dec 2007 20:47:47 -0800 From: mavdzee@yahoo.co.uk To: Boost-users@lists.boost.org Subject: [Boost-users] double arithmetic precession and equality
Hi,
I was wondering if boost provides any support for testing equality of two doubles. In principle in C/C++ you cannot rely on equality because it uses bitwise comparison of doubles and in arithmetic expressions results may have lost some precession. An example will help:
#include
int main(void) { double start = 3.123; double end = 4.124; double diff = end - start; // == 1.001 ???
if (diff != 1.001) { std::cout << diff << " and " << 1.001 << " are not equal" << std::endl; } }
On my machine, this example outputs: 1.001 and 1.001 are not equal
I am using a lot of arithmetic expressions on floating-point values and also need to test for equality. Also, at compile-time I do not know the precession so comparing within the range of an epsilon seems impossible.
So does boost have anything for this I don't know of? Or any other libraries?
Thanks, Andrej
___________________________________________________________ Yahoo! Answers - Got a question? Someone out there knows the answer. Try it now. http://uk.answers.yahoo.com/ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_________________________________________________________________ Share life as it happens with the new Windows Live. http://www.windowslive.com/share.html?ocid=TXT_TAGHM_Wave2_sharelife_122007
participants (3)
-
Andrej van der Zee
-
John Maddock
-
Mike Marchywka