Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing doubles

Tags:

c++

I'm writing a program that consists of a while loop that reads two doubles and prints them. The program also prints what the larger number is and what the smaller number is.

this is the code i have so far.

int main()
{

                                    // VARIABLE DECLARATIONS 

    double a;
    double b;

    while (a,b != '|')              //WHILE A & B DO NOT EQUAL '|'
    {
        cin >>a >>b;
        cout << a << b << "\n" ;


        if (a<b)                    //IF A<B: SMALLER VALUE IS A
        cout << "The smaller value is:" << a << endl 
             << "The larger value is:" << b << endl ;

        else if (b<a)               //ELSE IF B<A 
            cout << "The smaller value is:" << b << endl 
                 << "The larger value is:" << a << endl ;
        else if (b==a)
            cout << "The two numbers you entered are equal." << "\n" ;

    }
}

The next step is having the program write out "the numbers are almost equal" if the two numbers differ by less than 1.0/10000000. How would I do this?

like image 874
Capkutay Avatar asked Oct 24 '10 20:10

Capkutay


People also ask

Can you use == to compare doubles?

Using the == Operator As a result, we can't have an exact representation of most double values in our computers. They must be rounded to be saved. In that case, comparing both values with the == operator would produce a wrong result.

Can you compare double integers?

All int s are exactly representable as double .


2 Answers

Here is how I would test for equality, without a "fudge factor":

if (
    // Test 1: Very cheap, but can result in false negatives
    a==b || 
    // Test 2: More expensive, but comprehensive
    std::abs(a-b)<std::abs(std::min(a,b))*std::numeric_limits<double>::epsilon())
  std::cout << "The numbers are equal\n";

Explanation

The first test is a simple comparison. Of course we all know that comparing double precision values can result in them being deemed unequal, even when they are logically equivalent.

A double precision floating point value can hold the most significant fifteen digits of a number (actually ≈15.955 digits). Therefore, we want to call two values equal if (approximately) their first fifteen digits match. To phrase this another way, we want to call them equal if they are within one scaled epsilon of each other. This is exactly what the second test computes.

You can choose to add more leeway than a single scaled epsilon, due to more significant floating point errors creeping in as a result of iterative computation. To do this, add an error factor to the right hand side of the second test's comparison:

double error_factor=2.0;

if (a==b ||         
    std::abs(a-b)<std::abs(std::min(a,b))*std::numeric_limits<double>::epsilon()*
                  error_factor)
  std::cout << "The numbers are equal\n";

I cannot give you a fixed value for the error_factor, since it will depend on the amount of error that creeps into your computations. However, with some testing you should be able to find a reasonable value that suits your application. Do keep in mind that adding an (arbitrary) error factor based on speculation alone will put you right back into fudge factor territory.

Summary

You can wrap the following test into a(n inline) function:

inline bool logically_equal(double a, double b, double error_factor=1.0)
{
  return a==b || 
    std::abs(a-b)<std::abs(std::min(a,b))*std::numeric_limits<double>::epsilon()*
                  error_factor;
}
like image 125
Michael Goldshteyn Avatar answered Oct 17 '22 05:10

Michael Goldshteyn


I suggest the following article: new link
(obsolete link->Comparing floating point numbers)

like image 43
Tomek Avatar answered Oct 17 '22 03:10

Tomek