Daniele De Blu
Daniele De Blu

Reputation: 1

Approximation of double variables in C++

I'm trying to run this program, in which you insert two double values and you get in return which of the two numbers is bigger, a message "the numbers are equal" if the value of the two double are the same, and a message "the numbers are almost equal" if the difference between the two numbers is less than 1/10.000.000.

The code I wrote is this:

    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    inline void keep_window_open() { char ch; cin >> ch; }

    double square(double x)
    {
        return x * x;
    }

    int main() {
        double number;
        double number2;
        while (cin >> number, cin >> number2) {
            if (number == '|' || number2 == '|') break;
            else {
                cout << number << " " << number2 << endl;
                if (number > number2) cout << number << " is bigger than " << number2 << endl;
                if (number < number2) cout << number2 << " is bigger than " << number << endl;
                double difference = number - number2;
                if (difference > - 1/10000000 && difference < 0) cout << "the numbers are almost equal" << endl;
                if (difference < 1 / 10000000 && difference > 0) cout << "the numbers are almost equal" << endl;
                if (number == number2) cout << "the numbers are equal" << endl;
            }

        }
    }

When I run this program and insert two numbers that have a difference smaller than 1/10.000.000, the program should return "the numbers are almost equal", but it doesn't.

That's what it returns (when I run the program and insert two numbers) if I insert "1" and "1.0000000000001":

    "1 1.0000000000001

    1 1

    1 is bigger than 1"

I don't get why the program considers the two numbers which I insered to be 1, and returns "1" and "1" as values.

It also should give in return "the numbers are almost equal" since I wrote:

     if (difference > - 1/10000000 && difference < 0) cout << "the numbers are almost equal" << endl;
                        if (difference < 1 / 10000000 && difference > 0) cout << "the numbers are almost equal" << endl;

But it doesn't.

How do I get the program to return "the numbers are almost equal" when I insert two double values which differ by less than 1/10.000.000 (like for example "1" and "1.0000000000001")?

Upvotes: 0

Views: 2364

Answers (1)

raxerz
raxerz

Reputation: 576

cout rounds off such double values when you try to print them. See here

As for your code, the integer division (1/10000000) will always return an integer so you need to perform division of double or decimal values (1.0/10000000.0)

Here is a working code with some minor changes to the logic

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
inline void keep_window_open() { char ch; cin >> ch; }

double square(double x)
{
    return x * x;
}

int main() {
    double number;
    double number2;
    while (cin >> number, cin >> number2) {
        if (number == '|' || number2 == '|') break;
        else {
            cout.precision(15);
            cout << number << " " << number2 << endl;
            double difference = number - number2;
            if (difference > - 1/10000000.0 && difference < 0) cout << "the numbers are almost equal" << endl;
            else if (difference < 1/10000000.0 && difference > 0) cout << "the numbers are almost equal" << endl;
            else if (number == number2) cout << "the numbers are equal" << endl;
            else if (number > number2) cout << number << " is bigger than " << number2 << endl;
            else if (number < number2) cout << number2 << " is bigger than " << number << endl;
        }

    }
}

Upvotes: 1

Related Questions