I\'m working on a scientific computation & visualization project in C#/.NET, and we use doubles to represent all the physical quantities. Since floating-po
Your best answer is always better algorithms, of course. But it seems to me that if your values aren't all within a couple of orders of magnitude of 1, the using a fixed epsilon is not a good strategy. What you want to do instead is to insure that the values are equal to within some reasonable precision.
// are values equal to within 12 (or so) digits of precision?
//
bool ApproxEquals(double d1, double d2) {
return Math.Abs(d1 - d2) < (Math.Abs(d1) * 1e-12);
}
If this were C++, then you could also pull some tricks to compare the mantissa and exponent separately, but I can't think of any way to do this safely in unmanged code.