Re: comparison on non-integer types



On Sep 6, 7:46 pm, pete <pfil...@xxxxxxxxxxxxxx> wrote:
user923005 wrote:

On Sep 5, 6:20 pm, pete <pfil...@xxxxxxxxxxxxxx> wrote:
user923005 wrote:

On Sep 5, 11:20 am, Flash Gordon <s...@xxxxxxxxxxxxxxxxxx> wrote:
The basic problem is that computers are finite.
Therefore when working
with float or double you tend to get only approximate results.
Exactly what you are doing with them
and the vagaries of your particular
implementation will determine how fast and in what direction those
errors grow. Then there is the question of
whether it is better for your
equality test to return true for numbers
that theoretically should be
different or false for numbers that theoretically be equal.

I suggest you look at
the section of the comp.lang.c FAQ at
http://c-faq.com/thatisall about floating point numbers.
It is by no means exhaustive, but it is a start.

I believe I have posted this before:

#include <float.h>
#include <math.h>

int double_compare (double d1, double d2)
{
if (d1 > d2)
if ((d1 - d2) < fabs (d1 * DBL_EPSILON))
return 0;
else
return 1;
if (d1 < d2)
if ((d2 - d1) < fabs (d2 * DBL_EPSILON))
return 0;
else
return -1;
return 0;
}

int float_compare (float d1, float d2)
{
if (d1 > d2)
if ((d1 - d2) < fabsf (d1 * FLT_EPSILON))
return 0;
else
return 1;
if (d1 < d2)
if ((d2 - d1) < fabsf (d2 * FLT_EPSILON))
return 0;
else
return -1;
return 0;
}

That way, it's possible to have a number
which compares equal to two other numbers,
which do not compare equal to each other.

The other way, it is possible to have two numbers which are the same
mathematically but which do not compare the same. Is that better?

There's no place for EPSILON's
if you're writing a qsort compar function for an array of floats.

I disagree. The above comparison functions will produce potentially a
different sort order. But not one that is less correct. And in
general, a comparision without a sphere of uncertainty is not a
correct comparison for floating point.

Those functions can produce a different sort order
depending on which algorithm the sorting function uses.
For a qsort compar function,
the ordering of the array must be completely
defined by the compar functions

N869
7.20.5 Searching and sorting utilities
[#4] When the same objects (consisting of size bytes,
irrespective of their current positions in the array) are
passed more than once to the comparison function, the
results shall be consistent with one another. That is, for
qsort they shall define a total ordering on the array, and
for bsearch the same object shall always compare the same
way with the key.

Of course it is also possible that:

double x = get_user_input();
if (x == x) return 0;
return 1;

will return 1. If there is an interrupt between when the first copy
of x gets pushed into a floating point register and the second copy
gets pushed into a floating point register, and if the machine has
extended precision (e.g 80 bits for Intel CPUs) and is in a mode where
the extra precision can be used, then one of the instances may get
some bits trimmed off and the comparison will say unequal.

Using the normal relational operators does not reliably solve the
problem. That may be the reason that for OpenVMS floating point is
not even allowed in a key field by the operating system.


.



Relevant Pages

  • Re: rounding off double values
    ... while the second array is result of some calculation ... compare elements of both arrays .How do i do the rounding off? ... That's where floating points becomes scary, ... zero than far from zero, but this seems to measure whether the program ...
    (comp.lang.java.programmer)
  • Re: C++ more efficient than C?
    ... for x. bsearch() will compare it with x+2e, ... then you found the number that you were searching for, if it is floating ... says there is no such value in the array. ...
    (comp.lang.c)
  • Re: comparison on non-integer types
    ... with float or double you tend to get only approximate results. ... mathematically but which do not compare the same. ... if you're writing a qsort compar function for an array of floats. ...
    (comp.lang.c)
  • Re: comparison on non-integer types
    ... with float or double you tend to get only approximate results. ... mathematically but which do not compare the same. ... if you're writing a qsort compar function for an array of floats. ... correct comparison for floating point. ...
    (comp.lang.c)
  • hi
    ... contains floating points value.i have to store the data by using array ... of structure.i have to compare the previous value and check the ...
    (microsoft.public.vc.mfc)