Re: comparison on non-integer types
- From: user923005 <dcorbit@xxxxxxxxx>
- Date: Thu, 06 Sep 2007 20:55:13 -0700
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.
.
- Follow-Ups:
- Re: comparison on non-integer types
- From: Charlie Gordon
- Re: comparison on non-integer types
- References:
- comparison on non-integer types
- From: Pietro Cerutti
- Re: comparison on non-integer types
- From: Flash Gordon
- Re: comparison on non-integer types
- From: user923005
- Re: comparison on non-integer types
- From: pete
- Re: comparison on non-integer types
- From: user923005
- Re: comparison on non-integer types
- From: pete
- comparison on non-integer types
- Prev by Date: Re: c without usin any graphics function
- Next by Date: Re: draw a rectangular grid having 10 rows and 10 columns
- Previous by thread: Re: comparison on non-integer types
- Next by thread: Re: comparison on non-integer types
- Index(es):
Relevant Pages
|