Writing a macro for checking close enough floating points.

Discussion in 'C Programming' started by pereges, Jun 29, 2008.

  1. pereges

    pereges Guest

    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
     
    pereges, Jun 29, 2008
    #1
    1. Advertising

  2. On 29 Jun, 12:29, pereges <> 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, Jul 22, 2008
    #2
    1. Advertising

  3. the error rate in my posts seems to be climbing...


    On 22 Jul, 15:52, Nick Keighley <>
    wrote:
    > On 29 Jun, 12:29, pereges <> 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
     
    Nick Keighley, Jul 23, 2008
    #3
  4. pereges

    ~Glynne Guest

    On Jun 29, 5:29 am, pereges <> 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.

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

    ~Glynne
     
    ~Glynne, Jul 23, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anders K. Jacobsen [DK]

    Problem with floating points in datagrid

    Anders K. Jacobsen [DK], Apr 7, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    357
    Karl Seguin
    Apr 7, 2005
  2. =?Utf-8?B?bWF2cmlja18xMDE=?=

    is Connection.close() enough or

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Jun 27, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    3,962
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Jun 28, 2006
  3. pereges
    Replies:
    7
    Views:
    342
    Barry Schwarz
    May 31, 2008
  4. pereges
    Replies:
    8
    Views:
    349
    Flash Gordon
    Jul 22, 2008
  5. Iñaki Baz Castillo
    Replies:
    7
    Views:
    939
    Iñaki Baz Castillo
    Jan 12, 2010
Loading...

Share This Page