-1
int main() {
    double number1;
    double number2;
    double difference;
    const double close = 0.01;

    cout << "enter two numbers" << endl;

    while (cin >> number1 >> number2) {

        if (number1 > number2) {

            cout << "the smaller value is: " << number2 << endl;
            cout << "the larger value is: " << number1 << endl;

        } else if(number1 < number2) {

            cout << "the smaller value is: " << number1 << endl;
            cout << "the larger value is: " << number2 << endl;

        } else {

            cout << "the numbers are equal" << endl;

        }

        difference = abs(number1 - number2);
        cout << "the difference is: " << difference << endl << endl;

        if (difference == close) {

            cout << "since the difference is: " << difference << " it is almost the same number" << endl;
        }

        cout << "Enter two numbers: " <<  endl;
    }


    keep_window_open();

    return 0;
}

image

Why does it detect that the number is "almost the same" when I use inputs 0 and 0.01, but it does not write out the same message when the input is anything else like 5.59 and 5.6?

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • After you fix the bug identified in the answer I think you should look at this: [https://stackoverflow.com/questions/588004/is-floating-point-math-broken](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – drescherjm Jun 19 '20 at 00:50
  • The difference between 5.6 and 5.59 is probably calculated as close but not exactly 0.01. The link I posted above should help with this. – drescherjm Jun 19 '20 at 01:21
  • This doesn’t address the question, but you don’t need the extra stuff that ’std::endl’ does. `’\n’` ends a line – Pete Becker Jun 19 '20 at 02:28

2 Answers2

4

The difference between 5.59 and 5 is 0.59 which is not equal to 0.01. Perhaps you meant:

        if (difference <= close) {
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 0.59 is also not <= 0.01 ;) but yes thats most likely what OP wants – 463035818_is_not_an_ai Jun 18 '20 at 23:56
  • 2
    It looks like the original question had (at least) three characters cut off, ending with "5.59 and 5" instead of "5.59 and 5.6?" (This was not readily apparent until someone embedded the image into the question.) – JaMiT Jun 19 '20 at 01:55
2

Actually, c++ double is "Approximation" of the real number in some range, implemented with mantissa and exponent part. With this representations, only some numbers can be exact, and other numbers are just approximated. Therefore, 0.01, 5.59, and 0.59 are likely to be stored not exactly. That makes subtle behavior, and you should be careful when comparing the floating-point based number system.

K.R.Park
  • 1,015
  • 1
  • 4
  • 18
  • 1
    This is, I believe, the behaviour the asker intends to get from `difference == close`. In other words, they know this. They just messed up the implementation. – user4581301 Jun 19 '20 at 01:09
  • 1
    I am not so sure. I think they got confused when the difference was .01 in both examples and displayed as such but only the message printed in one of them. – drescherjm Jun 19 '20 at 01:20