compare two float values

Discussion in 'C Programming' started by neha_chhatre@yahoo.co.in, Feb 20, 2008.

  1. Guest

    can anybody tell me how to compare two float values
    say for example
    t and check are two variables declared float how to compare t and
    check
    please help me as soon as possible
    , Feb 20, 2008
    #1
    1. Advertising

  2. wrote:
    > can anybody tell me how to compare two float values
    > say for example
    > t and check are two variables declared float how to compare t and
    > check
    > please help me as soon as possible

    float t, check;
    ....
    if ( t > check)
    printf("%f is greater than %f\n" t, check);
    else
    printf("%f is less than %f\n" t, check);

    Don't compare for equality, won't work for float (or double), rather compare
    the difference against some delta that suits your needs for accuracy

    Bye, Jojo
    Joachim Schmitz, Feb 20, 2008
    #2
    1. Advertising

  3. Joachim Schmitz wrote:
    > wrote:
    >> can anybody tell me how to compare two float values
    >> say for example
    >> t and check are two variables declared float how to compare t and
    >> check
    >> please help me as soon as possible

    > float t, check;
    > ...
    > if ( t > check)
    > printf("%f is greater than %f\n" t, check);
    > else
    > printf("%f is less than %f\n" t, check);
    >
    > Don't compare for equality, won't work for float (or double), rather
    > compare the difference against some delta that suits your needs for
    > accuracy

    oops, epsilon, not delta, off by one in greek alphabeth...
    Joachim Schmitz, Feb 20, 2008
    #3
  4. Guest

    actually i want to check wether two float values are equal or not
    equal
    i.e suppose t and t1 are two float values and i want to check wether t
    and t1 are equal or not

    please send in syntax form as in code form

    please tell me how to do,
    , Feb 20, 2008
    #4
  5. Guest

    On Feb 20, 6:47 am, "Joachim Schmitz" <>
    wrote:
    > wrote:
    > > can anybody tell me how to compare two float values
    > > say for example
    > > t and check are two variables declared float how to compare t and
    > > check
    > > please help me as soon as possible

    >
    > float t, check;
    > ...
    > if ( t > check)
    >     printf("%f is greater than %f\n" t, check);
    > else
    >     printf("%f is less than %f\n" t, check);
    >
    > Don't compare for equality, won't work for float (or double), rather compare
    > the difference against some delta that suits your needs for accuracy
    >
    > Bye, Jojo


    Of course it will work:
    if ( t == check ) {...}
    WILL execute the block if the float value 't' is equal to the
    float value 'check'.

    It depends on how the values for 't' and 'check' were assigned
    whether this is accurate enough to provide the desired results.
    For example:

    t = 10.0;
    check = 5.0 + 5.0;

    The above test will work, since the two values will
    be identical.

    However, for:
    t = 11.0;
    check = 10 * 1.1;

    the above comparison (t==check), will most likely NOT work
    to determine whether these are mathematically identical.


    Bottom line: using the == operator will always check for
    exact equality of the two entities. However, either of the
    two entities being checked (or both) may not represent the
    exact mathematical value desired, and will probably
    differ if their calcualtion and assignment were not
    identical.
    --
    Fred Kleinschmidt
    , Feb 20, 2008
    #5
  6. Mark Bluemel Guest

    wrote:
    > actually i want to check wether two float values are equal or not
    > equal
    > i.e suppose t and t1 are two float values and i want to check wether t
    > and t1 are equal or not
    >
    > please send in syntax form as in code form


    Smells like homework to me...

    have you looked at the FAQ (http://www.c-faq.com)? There's a whole
    section on floating point numbers, and question 14.5 is probably
    close to what you want.
    Mark Bluemel, Feb 20, 2008
    #6
  7. wrote:
    > On Feb 20, 6:47 am, "Joachim Schmitz" <>
    > wrote:
    >> wrote:
    >>> can anybody tell me how to compare two float values
    >>> say for example
    >>> t and check are two variables declared float how to compare t and
    >>> check
    >>> please help me as soon as possible

    >>
    >> float t, check;
    >> ...
    >> if ( t > check)
    >> printf("%f is greater than %f\n" t, check);
    >> else
    >> printf("%f is less than %f\n" t, check);
    >>
    >> Don't compare for equality, won't work for float (or double), rather
    >> compare the difference against some delta that suits your needs for
    >> accuracy
    >>
    >> Bye, Jojo

    >
    > Of course it will work:
    > if ( t == check ) {...}
    > WILL execute the block if the float value 't' is equal to the
    > float value 'check'.
    >
    > It depends on how the values for 't' and 'check' were assigned
    > whether this is accurate enough to provide the desired results.
    > For example:
    >
    > t = 10.0;
    > check = 5.0 + 5.0;
    >
    > The above test will work, since the two values will
    > be identical.
    >
    > However, for:
    > t = 11.0;
    > check = 10 * 1.1;
    >
    > the above comparison (t==check), will most likely NOT work
    > to determine whether these are mathematically identical.
    >
    >
    > Bottom line: using the == operator will always check for
    > exact equality of the two entities. However, either of the
    > two entities being checked (or both) may not represent the
    > exact mathematical value desired, and will probably
    > differ if their calcualtion and assignment were not
    > identical.

    That's what I meant.

    Bye, Jojo
    Joachim Schmitz, Feb 20, 2008
    #7
  8. wrote:
    >
    > actually i want to check wether two float values are equal or not
    > equal
    > i.e suppose t and t1 are two float values and i want to check wether t
    > and t1 are equal or not
    >
    > please send in syntax form as in code form
    >
    > please tell me how to do,


    How would you check for equality if t and t1 were "int"?

    How would you check for equality if t and t1 were "short"?

    How would you check for equality if t and t1 were "long"?

    Now, how would you check for equality if t and t1 were "float"?

    Or did you have something else in mind for what "equal" means?

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Feb 20, 2008
    #8
  9. santosh Guest

    wrote:

    > actually i want to check wether two float values are equal or not
    > equal
    > i.e suppose t and t1 are two float values and i want to check wether t
    > and t1 are equal or not
    >
    > please send in syntax form as in code form
    >
    > please tell me how to do,


    Please quote relevant parts of the post to which you are replying. This
    is good practise on Usenet.

    Comparing floats for equality is fraught with danger as many values are
    not perfectly representible in binary floating point. You might find
    that you need to allow for a small variation on either side of the
    comparison value, just like the margin of error in scientific
    experiments.

    You might want to read:

    <http://www.cygnus-software.com/papers/comparingfloats/Comparing%20floating%20point%20numbers.htm>
    <http://docs.sun.com/source/806-3568/ncg_goldberg.html>
    santosh, Feb 20, 2008
    #9
  10. Kenneth Brody wrote:
    > wrote:
    >> actually i want to check wether two float values are equal or not
    >> equal
    >> i.e suppose t and t1 are two float values and i want to check wether t
    >> and t1 are equal or not
    >>
    >> please send in syntax form as in code form
    >>
    >> please tell me how to do,

    >
    > How would you check for equality if t and t1 were "int"?
    >
    > How would you check for equality if t and t1 were "short"?
    >
    > How would you check for equality if t and t1 were "long"?
    >
    > Now, how would you check for equality if t and t1 were "float"?


    In a different way than above....

    >
    > Or did you have something else in mind for what "equal" means?
    >



    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp
    Pietro Cerutti, Feb 20, 2008
    #10
  11. Kenneth Brody wrote:
    > wrote:
    >> actually i want to check wether two float values are equal or not
    >> equal
    >> i.e suppose t and t1 are two float values and i want to check wether t
    >> and t1 are equal or not
    >>
    >> please send in syntax form as in code form
    >>
    >> please tell me how to do,

    >
    > How would you check for equality if t and t1 were "int"?
    >
    > How would you check for equality if t and t1 were "short"?
    >
    > How would you check for equality if t and t1 were "long"?
    >
    > Now, how would you check for equality if t and t1 were "float"?


    In a different way than above....

    >
    > Or did you have something else in mind for what "equal" means?
    >



    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp
    Pietro Cerutti, Feb 20, 2008
    #11
  12. CBFalconer Guest

    wrote:
    >
    > actually i want to check wether two float values are equal or not
    > equal. i.e suppose t and t1 are two float values and i want to
    > check wether t and t1 are equal or not


    #include <float.h> /* FLT_EPSILON */
    #include <math.h> /* fabsf() */

    int floatequal(float t, float t1) {

    float criterion;

    criterion = (fabsf(t) + fabsf(t1)) * FLT_EPSILON;
    if (fabsf(t - t1) > criterion) return 0; /* not equal */
    else return 1; /* equal */
    } /* untested */

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Feb 20, 2008
    #12
  13. Guest

    thank you for giving me the site url
    , Feb 20, 2008
    #13
  14. Eric Sosman Guest

    CBFalconer wrote:
    > wrote:
    >> actually i want to check wether two float values are equal or not
    >> equal. i.e suppose t and t1 are two float values and i want to
    >> check wether t and t1 are equal or not

    >
    > #include <float.h> /* FLT_EPSILON */
    > #include <math.h> /* fabsf() */
    >
    > int floatequal(float t, float t1) {
    >
    > float criterion;
    >
    > criterion = (fabsf(t) + fabsf(t1)) * FLT_EPSILON;
    > if (fabsf(t - t1) > criterion) return 0; /* not equal */
    > else return 1; /* equal */
    > } /* untested */


    Needs a little more work for NaNs and infinities ...

    More to the point, the choice of a proper epsilon is
    not dictated solely by the machine's precision, but also
    by the computations that produced the numbers. For example,
    if you calculate a binomial coefficient as

    exp(lgamma(n)-lgamma(m)-lgamma(n-m))*n/(m*(n-m))

    .... the accuracy of the result will quite likely depend a
    good deal on the accuracies of the four transcendental
    function evaluations, as well as on the fundamental precision
    of the floating-point representation. An "approximately
    equal" comparison based solely on the machine's epsilon might
    lead one to conclude that the number five-card poker hands
    (52 choose 5 == 52!/(5!*47!) == 2598960) is not an integer!

    --
    Eric Sosman, Feb 20, 2008
    #14
  15. user923005 Guest

    On Feb 20, 6:42 am, wrote:
    > can anybody tell me how to compare two float values
    > say for example
    > t and check are two variables declared float how to compare t and
    > check
    > please help me as soon as possible


    This should be a FAQ.

    #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+2, a, DBL_DIG+2, b,
    double_compare (a, b));
    a *= -1;
    b *= -1;
    printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, a, DBL_DIG+2, 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+2, u, DBL_DIG+2, v,
    double_compare (u, v));


    u *= big;
    v *= big;

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

    return 0;
    }

    #endif
    user923005, Feb 20, 2008
    #15
  16. Kenneth Brody wrote:
    >
    > How would you check for equality if t and t1 were "int"?
    > Now, how would you check for equality if t and t1 were "float"?


    By some different means. Two mathematically identical floats may compare
    inequal by obvious methods.

    > Or did you have something else in mind for what "equal" means?


    Doubtful, but he also probably expected the below to print "true".

    #include <stdio.h>
    #include <math.h>
    int main(void)
    {
    printf("%f %f %s\n",
    exp(log(4.444)), 4.444,
    exp(log(4.444))==4.444?"true":"false");
    return 0;
    }

    $ ./a.out
    4.444000 4.444000 false

    --
    Mark McIntyre

    CLC FAQ <http://c-faq.com/>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
    Mark McIntyre, Feb 20, 2008
    #16
  17. CBFalconer Guest

    Eric Sosman wrote:
    > CBFalconer wrote:
    >> wrote:
    >>
    >>> actually i want to check wether two float values are equal or
    >>> not equal. i.e suppose t and t1 are two float values and i
    >>> want to check wether t and t1 are equal or not

    >>
    >> #include <float.h> /* FLT_EPSILON */
    >> #include <math.h> /* fabsf() */
    >>
    >> int floatequal(float t, float t1) {
    >>
    >> float criterion;
    >>
    >> criterion = (fabsf(t) + fabsf(t1)) * FLT_EPSILON;
    >> if (fabsf(t - t1) > criterion) return 0; /* not equal */
    >> else return 1; /* equal */
    >> } /* untested */

    >
    > Needs a little more work for NaNs and infinities ...
    >
    > More to the point, the choice of a proper epsilon is
    > not dictated solely by the machine's precision, but also
    > by the computations that produced the numbers. For example,
    > if you calculate a binomial coefficient as


    Ignoring NaNs and infinities, the criterion I used is sufficient to
    guarantee that an equal/nonequal decision can be made. At most
    that criterion is too large by a factor of two, giving an excessive
    range of equality. BTW, I believe there is no guarantee that NaNs
    and infinities exist in the floating point system.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Feb 21, 2008
    #17
  18. CBFalconer Guest

    user923005 wrote:
    > wrote:
    >
    >> can anybody tell me how to compare two float values, say for
    >> example t and check are two variables declared float how to
    >> compare t and check

    >
    > This should be a FAQ.
    >
    > #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;
    > }


    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. :)

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Feb 21, 2008
    #18
  19. user923005 Guest

    On Feb 20, 5:00 pm, CBFalconer <> wrote:
    > user923005 wrote:
    > > wrote:

    >
    > >> can anybody tell me how to compare two float values, say for
    > >> example t and check are two variables declared float how to
    > >> compare t and check

    >
    > > This should be a FAQ.

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

    >
    > 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. :)


    Try it.
    user923005, Feb 21, 2008
    #19
  20. user923005 Guest

    On Feb 20, 5:00 pm, CBFalconer <> wrote:
    > user923005 wrote:
    > > wrote:

    >
    > >> can anybody tell me how to compare two float values, say for
    > >> example t and check are two variables declared float how to
    > >> compare t and check

    >
    > > This should be a FAQ.

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

    >
    > 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. :)


    I would be very interested to see any instance where this compare
    function fails to perform as expected.

    C:\tmp>cl /DUNIT_TEST /W4 /Ox fcomp.c
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762
    for 80x86
    Copyright (C) Microsoft Corporation. All rights reserved.

    fcomp.c
    Microsoft (R) Incremental Linker Version 8.00.50727.762
    Copyright (C) Microsoft Corporation. All rights reserved.

    /out:fcomp.exe
    fcomp.obj

    C:\tmp>fcomp
    compare( 1, 1) = 0.
    compare( -1, -1) = 0.
    compare( 1, 1) = -1.
    compare( 1.84467e+019, 1.84467e+019) = -1.
    compare( 2.22045e-016, -1.79769e+308) = 1.

    C:\tmp>type fcomp.c
    #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+2, a, DBL_DIG+2, b,
    double_compare (a, b));
    a *= -1;
    b *= -1;
    printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, a, DBL_DIG+2, 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+2, u, DBL_DIG+2, v,
    double_compare (u, v));


    u *= big;
    v *= big;

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

    u = DBL_EPSILON;
    v = -DBL_MAX;
    printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, u, DBL_DIG+2, v,
    double_compare (u, v));

    return 0;
    }

    #endif
    user923005, Feb 21, 2008
    #20
    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. Sukh
    Replies:
    3
    Views:
    76,468
    sloan
    Feb 6, 2006
  2. bd
    Replies:
    0
    Views:
    604
  3. GenxLogic
    Replies:
    3
    Views:
    1,242
    andrewmcdonagh
    Dec 6, 2006
  4. k3n3dy
    Replies:
    15
    Views:
    973
    dan2online
    Apr 20, 2006
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,505
    James Kanze
    Oct 8, 2009
Loading...

Share This Page