compare two float values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • neha_chhatre@yahoo.co.in

    compare two float values

    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
  • Joachim Schmitz

    #2
    Re: compare two float values

    neha_chhatre@ya hoo.co.in 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


    Comment

    • Joachim Schmitz

      #3
      Re: compare two float values

      Joachim Schmitz wrote:
      neha_chhatre@ya hoo.co.in 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...


      Comment

      • neha_chhatre@yahoo.co.in

        #4
        Re: compare two float values

        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,

        Comment

        • Mark Bluemel

          #5
          Re: compare two float values

          neha_chhatre@ya hoo.co.in 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.

          Comment

          • santosh

            #6
            Re: compare two float values

            neha_chhatre@ya hoo.co.in 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%20flo ating%20point%2 0numbers.htm>
            <http://docs.sun.com/source/806-3568/ncg_goldberg.ht ml>

            Comment

            • Kenneth Brody

              #7
              Re: compare two float values

              neha_chhatre@ya hoo.co.in 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:ThisIsA SpamTrap@gmail. com>

              Comment

              • Pietro Cerutti

                #8
                Re: compare two float values

                Kenneth Brody wrote:
                neha_chhatre@ya hoo.co.in 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:

                Comment

                • Pietro Cerutti

                  #9
                  Re: compare two float values

                  Kenneth Brody wrote:
                  neha_chhatre@ya hoo.co.in 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:

                  Comment

                  • CBFalconer

                    #10
                    Re: compare two float values

                    neha_chhatre@ya hoo.co.in 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(floa t 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

                    Comment

                    • neha_chhatre@yahoo.co.in

                      #11
                      Re: compare two float values

                      thank you for giving me the site url

                      Comment

                      • Eric Sosman

                        #12
                        Re: compare two float values

                        CBFalconer wrote:
                        neha_chhatre@ya hoo.co.in 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(floa t 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 "approximat ely
                        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@sun .com

                        Comment

                        • user923005

                          #13
                          Re: compare two float values

                          On Feb 20, 6:42 am, neha_chha...@ya hoo.co.in 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.9999999999999 9999999999999;
                          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

                          Comment

                          • Mark McIntyre

                            #14
                            Re: compare two float values

                            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 >

                            Comment

                            • CBFalconer

                              #15
                              Re: compare two float values

                              user923005 wrote:
                              neha_chha...@ya hoo.co.in 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

                              Comment

                              Working...