How to determine if two numbers differ from a specific value

Hello, in chapter 4 in the book Programming principles and practice using C++ I have to write a program that use a while loop and that each time around the loop reads two double, prints out the largest, the smallest and if they are equal.
I have no problem with this program but I can't understand how to modify it following the author : Change the program so that it writes out "the numbers are almost equal" after writing out which is the larger and the smaller if the two numbers differ by less than 1.0/100.

for now this is my code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

int main()	
{
	double a = 0; 
	double b = 0; 

	while (cin >> a >> b) {

		cout << "first number is : " << a << "\nsecond number is : " << b << "\n\n"; 
		
		if (a > b) {
			cout << "the larger value is : " << a << "\n";
			cout << "the smaller value is : " << b << "\n";
		
		}

		else if (b > a) {
			cout << "the larger value is : " << b << "\n";
			cout << "the smaller value is : " << a << "\n";
		}

		else
			cout << "The numbers are equal\n"; 


		if (a - b < 1.0 / 100)
			cout << "Numbers are almost equals"; 

	}// end of while 




The problem is that I don't know how to test if the numbers differ by less than 0.01 because my if statement doesn't work in any case. If I enter 2 and 1.99 It doesn't work, why ? I found people with the same problem but that have far more experience than me in programming, so I don't know why this statement doesn't work.Can you explain me why in a very simple way for a newbie ? Thank you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cmath>
#include <algorithm>

int main()
{
    const double EPSILON = 1.0 / 100 ;
    double a = 0;
    double b = 0;

    while ( std::cin >> a >> b ) {

        std::cout << "first number is : " << a << "\nsecond number is : " << b << "\n\n";

        if( a == b ) // check this first
            std::cout << "The numbers are equal\n";

        else { // not equal

            if ( a > b )
            {
                std::cout << "the larger value is : " << a << '\n';
                std::cout << "the smaller value is : " << b << '\n';
            }

            else   // a < b
            {
                std::cout << "the larger value is : " << b << '\n';
                std::cout << "the smaller value is : " << a << '\n';
            }

            // http://en.cppreference.com/w/cpp/numeric/math/fabs
            const double difference = std::abs( a - b ) ; // absolute value of difference

            if( difference < EPSILON ) std::cout << "Numbers are almost equal\n" ;

        } // not equal

    }// end of while
}
Last edited on

#include <iostream>
#include <cmath>
#include <algorithm>

int main()
{
const double EPSILON = 1.0 / 100 ;
double a = 0;
double b = 0;

while ( std::cin >> a >> b ) {

std::cout << "first number is : " << a << "\nsecond number is : " << b << "\n\n";

if( a == b ) // check this first
std::cout << "The numbers are equal\n";

else { // not equal

if ( a > b )
{
std::cout << "the larger value is : " << a << '\n';
std::cout << "the smaller value is : " << b << '\n';
}

else // a < b
{
std::cout << "the larger value is : " << b << '\n';
std::cout << "the smaller value is : " << a << '\n';
}

// http://en.cppreference.com/w/cpp/numeric/math/fabs
const double difference = std::abs( a - b ) ; // absolute value of difference

if( difference < EPSILON ) std::cout << "Numbers are almost equal\n" ;

} // not equal

}// end of while
}




@JLborges why if I enter 10 and 10.01 I don't get : numbers are almost equal ? what input should I give to the program to get the correct result ?
Because difference between 10 and 10.01 is exactly 0.01 and not less than that.

Enter 4.2467 and 4.2434 for example.

EDIT: also floating point imprecision can lead to 10 and 10.01 outputting said line for some people.
Last edited on
> why if I enter 10 and 10.01 I don't get : numbers are almost equal ?

I do get: numbers are almost equal.
http://coliru.stacked-crooked.com/a/4a01feda0323161f

That being said, the floating point representation is an inexact representation of real numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <iomanip>

int main()
{
    double a = 10.01 ;
    double b = 10.00 ;

    // this may not (is very unlikely to) give 0.01 as the result.
    // it will be quite close to 0.01, but may not be exactly 0.01
    // could be either a wee bit lower than 0.01 or a wee bit higher than 0.01
    
    std::cout << std::fixed << std::setprecision(16) << a-b << '\n' ; // 0.0099999999999998

    a = 100.01 ;
    b = 100.00 ;
    std::cout << a-b << '\n' ; // 0.0100000000000051

    a = 1000.01 ;
    b = 1000.00 ;
    std::cout << a-b << '\n' ; // 0.0099999999999909

    a = 1000000.01 ;
    b = 1000000.00 ;
    std::cout << a-b << '\n' ; // 0.0100000000093132
}

http://coliru.stacked-crooked.com/a/5978ee75a4db736a


> what input should I give to the program to get the correct result ?

Try 10 and 10.00999999
Last edited on
Topic archived. No new replies allowed.