468,510 Members | 1,768 Online

# Writing a macro for checking close enough floating points.

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
Jun 29 '08 #1
3 2203 On 29 Jun, 12:29, pereges <Brol...@gmail.comwrote:
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
Jul 22 '08 #2
the error rate in my posts seems to be climbing...
On 22 Jul, 15:52, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
On 29 Jun, 12:29, pereges <Brol...@gmail.comwrote:
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

Jul 23 '08 #3
On Jun 29, 5:29*am, pereges <Brol...@gmail.comwrote:
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.

Download it from
http://fcmp.sourceforge.net/

~Glynne
Jul 23 '08 #4

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 25 posts views Thread by Andrew Dalke | last post: by 69 posts views Thread by Ken | last post: by 11 posts views Thread by Mr. Smith | last post: by 14 posts views Thread by Michael B Allen | last post: by 13 posts views Thread by Bern McCarty | last post: by 2 posts views Thread by simonc | last post: by 12 posts views Thread by Fabrice | last post: by 6 posts views Thread by jason | last post: by 6 posts views Thread by pereges | last post: by 9 posts views Thread by pereges | last post: by reply views Thread by ravipankaj | last post: by reply views Thread by ravipankaj | last post: by reply views Thread by slotstar | last post: by reply views Thread by captainhaddock | last post: by 1 post views Thread by fmendoza | last post: by 3 posts views Thread by Davism4 | last post: by 1 post views Thread by sonnet | last post: by reply views Thread by jimatqsi | last post: by 1 post views Thread by hwsilver | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.