Usenet.com

www.Usenet.com

Group Index

Comp Thread Archive from Usenet.com

<-- __Chronological__ --> <-- __Thread__ -->

Re: Inexact floating-point compare



Fred J. Tydeman wrote:

Does anyone know of any machines, that have a floating-point compare
instruction, where that instruction is not exact? That is, some of
the value bits of the floating-point numbers are ignored. Or, if
the compare is done internally as a subtract, the subtract results in
an underflow (so two different numbers near the minimum normalized
value, when subtracted produce a subnormal number) that is then flushed to zero, so compare equal.

The easiest method would seem to be a subtract, scale, add and compare:


double inexact_compare(double a, double b, int bits_to_skip)
{
  // First the the regular difference:
  double diff = b - a;

  // Shift this difference down by the number of bits to ignore:
  double scaled_diff = diff / ((double) (2 << bits_to_skip));

  // Add it back to a, this will be a NOP if
  // the difference is small enough
  double a_delta = a + scaled_diff;

  // Are the resulting values equal?
  if (a == a_delta) return 0.0;

  // Otherwise, return the actual difference:
  return diff;
}

For a fast implementation, the division and shift operation should of course be replaced with either a table lookup, or by sending the required scale factor as the third parameter:

double inexact_compare(double a, double b, double scale_factor)
...
  scaled_diff = diff * scale_factor;

Terje
--
- <[EMAIL PROTECTED]>
"almost all programming can be viewed as an exercise in caching"




<-- __Chronological__ --> <-- __Thread__ -->


Usenet.com



Please check out one of the premium Usenet Newsgroup Service Providers below for access to Usenet.