Hi Peter, I forwarded your excellent email to a workmate of mine, who had the following to say about it; I've Cc'ed him on the email. I'll put it inline in the email so I'm not top-posting :). (Note: Everything down here are Michael's thoughts, not mine! Hope you don't mind, Mike...)

On 16/11/2006, at 11:27 AM, Peter Miller wrote:

On Thu, 2006-11-16 at 10:46 +1100, Jamie Wilkinson wrote:

What's better:

 if expected - epsilon <= computation()<= expected + epsilon:

versus:

 if abs(computation() - expected) >= epsilon:

Epsilon if a function of the number of bits in the mantissa, but not the
number of bits of exponent.  So a better test tends to be

  if (fabs(computation()/expected) > 1 + epsilon)
    blah

From Michael Anderson:

The second equation is wrong! -
 if (fabs(computation()/expected) > 1 + epsilon)
   blah
With this test computation=-1 and expected=+1 passes for epsilon=0 ... not good.

but that makes matters worse, because the division loses more precision,
so the better but less obvious C code is

  int expected_exponent;
  frexp(expected, &expected_exponent);
  double scaled_epsilon = ldexp(unscaled_epsilon, expected_exponent);
  if (fabs(computation() - expected) >= scaled_epsilon)
    blah

From Michael Anderson:

Since I'd written my thoughts on the interval case I thought I'd pass them on too...

For the interval case...
Usually when I'm doing this kind of test I don't care about the "exact" result, its usually a stopping criteria of some algorithm - a convergence test or similar.
What I've always used in those cases is similar to

approxError = fabs(computation() - expected) / (1.0 + fabs(expected) +fabs(computation()) ) >= epsilon This is slower, but probably more robust over a wide range of epsilon, expected and computation.

Essentially its a trade of between absolute error (for small values) and relative error (for large values). Which is usually more appropriate than a direct comparison for my algorithms.


--
% Andre Pang (on behalf of Michael Anderson) <http:// www.algorithm.com.au/>


_______________________________________________
coders mailing list
coders@slug.org.au
http://lists.slug.org.au/listinfo/coders

Reply via email to