Comparing two floats or doubles to a precision

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • {AGUT2} {H}-IWIK

    Comparing two floats or doubles to a precision

    Hi,

    Is it possible to compare / output two numbers to a certain precision?

    for instance:

    #include <stdlib>
    #include <math>
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <vector>
    #include <string>
    #include <algorithm>

    main()
    {

    double one = 0.987432100
    double two = 0.987123456

    if ( one == two ) // [[ to a precision of 3 d.p.]]
    {
    std::cout << "YES! :D ";
    } else
    {
    cout << "No! >:( ";

    }

    Thanks,

    Alex :)

    --
    Reply to:alex an.ti livingstone sp@am btinternet.com cutting the usual...
  • Kevin Goodsell

    #2
    Re: Comparing two floats or doubles to a precision

    {AGUT2} {H}-IWIK wrote:
    [color=blue]
    > Hi,
    >
    > Is it possible to compare / output two numbers to a certain precision?
    >
    > for instance:
    >
    > #include <stdlib>
    > #include <math>
    > #include <iostream>
    > #include <iomanip>
    > #include <fstream>
    > #include <vector>
    > #include <string>
    > #include <algorithm>
    >
    > main()[/color]

    int main()

    There is no 'implicit int' rule in C++.
    [color=blue]
    > {
    >
    > double one = 0.987432100
    > double two = 0.987123456
    >
    > if ( one == two ) // [[ to a precision of 3 d.p.]][/color]

    You could write a function for it. But depending on exactly what you
    want to do it could be tricky. In this case you could probably multiply
    by 1000, convert to int, then compare. But this doesn't work well as a
    general solution because 1) int (or long) may not be wide enough for the
    result, and 2) the rounding method (truncation) may not be what you want.
    [color=blue]
    > {
    > std::cout << "YES! :D ";
    > } else
    > {
    > cout << "No! >:( ";[/color]

    std::cout

    Also, you are missing:

    }

    And finally, you need to end your program's output with a newline:

    std::cout << std::endl;
    [color=blue]
    >
    > }
    >
    > Thanks,
    >
    > Alex :)
    >[/color]

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.

    Comment

    • {AGUT2} {H}-IWIK

      #3
      Re: Comparing two floats or doubles to a precision

      >> int main()[color=blue]
      > Also, you are missing:
      > }
      > And finally, you need to end your program's output with a newline:
      > std::cout << std::endl;[/color]

      OK, I apologise PROFUSELY for my apalling syntax. :D I'm trying to jump in
      headfirst as that's how (I find) I learn the fastest. Your tips are
      appreciated.

      Also, thank you for your idea. However, after lots (and lots and lots) of
      Googling, I found the answer!

      I have got an

      if ( fabs(double1-double2) < 0.0000001 )
      {
      ....
      }

      In my program and this seems to work wonderfully.

      Thank you again for your time.

      Alex

      --
      Reply to:alex an.ti livingstone sp@am btinternet.com cutting the usual...
      If you fancy a chat, #agut on quakenet :)

      For all your UT2003 questions, visit the official UT2003 newsgroup FAQ at:

      Comment

      • Kevin Goodsell

        #4
        Re: Comparing two floats or doubles to a precision

        {AGUT2} {H}-IWIK wrote:[color=blue]
        >
        > if ( fabs(double1-double2) < 0.0000001 )[/color]

        Yeah, I should have thought of that.

        -Kevin
        --
        My email address is valid, but changes periodically.
        To contact me please use the address from a recent posting.

        Comment

        • Marcelo Pinto

          #5
          Re: Comparing two floats or doubles to a precision

          {AGUT2} {H}-IWIK <alexan.tilivin gstonesp@ambtin ternet.com> wrote in message news:<oprvczeki ip508op@mercury .nildram.net>.. .[color=blue]
          > I have got an
          >
          > if ( fabs(double1-double2) < 0.0000001 )
          > {
          > ...
          > }
          >
          > In my program and this seems to work wonderfully.
          >[/color]

          Note however that this is not the general rule. Consider the problem
          where you have the typical magnitude of values inferior to 0.0000001,
          then all your values will seem equal according to that rule.

          A more general solution would be to consider the magnitude of the
          numbers involved in the comparison:


          #include <iostream>

          bool equals(double d1, double d2, double precision);
          int main()
          {
          double a = 1, b = 1.0001;
          if (equals(a, b, 0.00001))
          std::cout << "equals" << std::endl;
          else
          std::cout << "differs" << std::endl;
          return 0;
          }

          bool equals(double d1, double d2, double precision)
          {
          double eps1 = fabs(d1), eps2 = fabs(d2), eps;
          eps = (eps1 > eps2) ? eps1 : eps2;
          if (eps == 0.0)
          return true; //both numbers are 0.0
          //eps hold the minimum distance between the values
          //that will be considered as the numbers are equal
          //considering the magnitude of the numbers
          eps *= precision;
          return (fabs(d1 - d2) < eps);
          }

          Comment

          Working...