Velocity Reviews > Writing a macro for checking close enough floating points.

# Writing a macro for checking close enough floating points.

pereges
Guest
Posts: n/a

 06-29-2008
Hi, I'm trying to write a macro for the relative difference function
which is used to check the close enough floating point values. Is
this correct way to write it ? :

#define EPSILON 0.000001
#define max(x, y) ((x) > (y) ? (x) : (y))
#define eq(a, b) max(fabs(a), fabs(b)) == 0.0 ? 0.0 : fabs(a - b) /
(max(fabs(a), fabs(b)))

Now for checking if two doubles, say x and y, are close we use eq(a,b)
<= EPSILON

Nick Keighley
Guest
Posts: n/a

 07-22-2008
On 29 Jun, 12:29, pereges <(E-Mail Removed)> wrote:

> Hi, I'm trying to write a macro for the relative difference function
> which is used to check the close enough floating point values. *Is
> this correct way to write it ? :
>
> #define EPSILON 0.000001
> #define max(x, y) ((x) > (y) ? *(x) : (y))
> #define eq(a, b) *max(fabs(a), fabs(b)) == 0.0 ? 0.0 : fabs(a - b) /
> (max(fabs(a), fabs(b)))
>
> Now for checking if two doubles, say x and y, are close we use eq(a,b)
> <= EPSILON

can you get almost-divide-by-ero if a and b are very small?
For instance what if
a == b == 1e-53 (or some other very non-zero number)

--
Nick Keighley

Nick Keighley
Guest
Posts: n/a

 07-23-2008
the error rate in my posts seems to be climbing...

On 22 Jul, 15:52, Nick Keighley <(E-Mail Removed)>
wrote:
> On 29 Jun, 12:29, pereges <(E-Mail Removed)> wrote:
>
> > Hi, I'm trying to write a macro for the relative difference function
> > which is used to check the close enough floating point values. *Is
> > this correct way to write it ? :

>
> > #define EPSILON 0.000001
> > #define max(x, y) ((x) > (y) ? *(x) : (y))
> > #define eq(a, b) *max(fabs(a), fabs(b)) == 0.0 ? 0.0 : fabs(a - b) /
> > (max(fabs(a), fabs(b)))

>
> > Now for checking if two doubles, say x and y, are close we use eq(a,b)
> > <= EPSILON

>
> can you get almost-divide-by-[z]ero if a and b are very small?
> For instance what if
> *a == b == 1e-53 (or some other very [small] non-zero number)
>
> --Nick Keighley

--
Nick Keighley

~Glynne
Guest
Posts: n/a

 07-23-2008
On Jun 29, 5:29*am, pereges <(E-Mail Removed)> wrote:
> Hi, I'm trying to write a macro for the relative difference function
> which is used to check the close enough floating point values. *Is
> this correct way to write it ? :
>
> #define EPSILON 0.000001
> #define max(x, y) ((x) > (y) ? *(x) : (y))
> #define eq(a, b) *max(fabs(a), fabs(b)) == 0.0 ? 0.0 : fabs(a - b) /
> (max(fabs(a), fabs(b)))
>
> Now for checking if two doubles, say x and y, are close we use eq(a,b)
> <= EPSILON

Just use Knuth's fcmp(). It accounts for a number of subtle issues,
which are (nearly) impossible to capture in a simple macro.

http://fcmp.sourceforge.net/

~Glynne

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post IĆ±aki Baz Castillo Ruby 7 01-12-2010 01:32 PM pereges C Programming 8 07-22-2008 01:32 PM pereges C Programming 7 05-31-2008 07:24 AM ajacobs2 Digital Photography 33 10-05-2003 12:14 PM VT Digital Photography 43 09-12-2003 11:15 AM