geo_decls.h oopsie... - Mailing list pgsql-hackers

From Dann Corbit
Subject geo_decls.h oopsie...
Date
Msg-id D90A5A6C612A39408103E6ECDD77B8290FD4E3@voyager.corporate.connx.com
Whole thread Raw
Responses Re: geo_decls.h oopsie...  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
These macros are from geo_decls.h:
#define EPSILON                    1.0E-06

#ifdef EPSILON
#define FPzero(A)                (fabs(A) <= EPSILON)
#define FPeq(A,B)                (fabs((A) - (B)) <=
EPSILON)
#define FPne(A,B)                (fabs((A) - (B)) >
EPSILON)
#define FPlt(A,B)                ((B) - (A) > EPSILON)
#define FPle(A,B)                ((A) - (B) <= EPSILON)
#define FPgt(A,B)                ((A) - (B) > EPSILON)
#define FPge(A,B)                ((B) - (A) <= EPSILON)
#else
#define FPzero(A)                ((A) == 0)
#define FPeq(A,B)                ((A) == (B))
#define FPne(A,B)                ((A) != (B))
#define FPlt(A,B)                ((A) < (B))
#define FPle(A,B)                ((A) <= (B))
#define FPgt(A,B)                ((A) > (B))
#define FPge(A,B)                ((A) >= (B))
#endif

But to compare floating point, those are simply wrong.  If the values
are both very small or both very large, the method fails.
Here is a more reliable way to make a comparison:

#include <float.h>
double          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;
 
}

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

All the macros can then be defined in terms of the comparison function.
It's not perfect either, but at least it is better.


pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: When and where to check for function permissions
Next
From: Tom Lane
Date:
Subject: Re: NAMEDATALEN Changes