0

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")?

  • 3
    '1/10000000' is zero. – Martin James Feb 24 '18 at 10:50
  • Comparisons `number == '|' || number2 == '|'` are useless. Doubles do not contain chars. – 273K Feb 24 '18 at 10:52
  • Keep in mind there are plenty of pairs of doubles with no double between them that are more than 1/10 million apart from each other. – chris Feb 24 '18 at 11:07
  • 1
    Change `1/10000000` to `1./10000000.` to do that division using doubles rather than integers and get the result you want (rather than 0). – Jesper Juhl Feb 24 '18 at 11:33
  • @S.M. `'|'` will be replaced by the numerical value (`124` if ASCII is used), so it will be true if you enter `124`. – mch Feb 24 '18 at 11:53
  • @mch Possibility does not make this expression useful. – 273K Feb 24 '18 at 12:00
  • This doesn't address the question, but the usual way to write `while (cin >> number, cin >> number2)` is `while (cin >> number >> number2)`. – Pete Becker Feb 24 '18 at 13:01

1 Answers1

1

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

    }
}
raxerz
  • 516
  • 4
  • 10