On Feb 20, 5:59*pm, user923005 <(E-Mail Removed)> wrote:

> On Feb 20, 5:00*pm, CBFalconer <(E-Mail Removed)> wrote:
[snip]

> > What if d1 == DBL_EPSILON and d2 == -LARGE_NUMBER? *I think you are

> > trying to do too much in one routine. *I also think I have the

> > foul-up condition fouled.
This version has better formatting, which will allow us to see the

dirt.

That makes the output much easier to grok.

#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;

}

#ifdef UNIT_TEST

#include <stdio.h>

int main ()

{

double u = 1.0;

double v = u + 1e-14;

double big = 1.0;

double a = 1.0, b = 0.99999999999999999999999999;

printf ("compare(%.*g, %.*g) = %d.\n", DBL_DIG+5, a, DBL_DIG+5, b,

double_compare (a, b));

a *= -1;

b *= -1;

printf ("compare(%.*g, %.*g) = %d.\n", DBL_DIG+5, a, DBL_DIG+5, b,

double_compare (a, b));

big *= 1 << 16;

big *= 1 << 16;

big *= 1 << 16;

big *= 1 << 16;

printf ("compare(%.*g, %.*g) = %d.\n", DBL_DIG+5, u, DBL_DIG+5, v,

double_compare (u, v));

u *= big;

v *= big;

printf ("compare(%.*g, %.*g) = %d.\n", DBL_DIG+5, u, DBL_DIG+5, v,

double_compare (u, v));

u = DBL_EPSILON;

v = -DBL_MAX;

printf ("compare(%.*g, %.*g) = %d.\n", DBL_DIG+5, u, DBL_DIG+5, v,

double_compare (u, v));

return 0;

}

/*

compare(1, 1) = 0.

compare(-1, -1) = 0.

compare(1, 1.00000000000001) = -1.

compare(18446744073709552000, 18446744073709736000) = -1.

compare(2.2204460492503131e-016, -1.7976931348623157e+30

= 1.

*/

#endif