Decimal Place Problem

Jun 9, 2020 at 1:47pm
Hi,
I am doing C++ exercises in a beginners text-book on C++. The chapter I am up to has to do with control structures (if, else if).

Anyway one of the exercises read as:

"Write a program that reads in two floating-point numbers and tests whether they are the same up to two decimal places. Here are two sample runs.

Enter two floating-point numbers: 2.0 1.99998
They are the same up to two decimal places.
Enter two floating-point numbers: 2.0 1.98999
They are different."


So to me the question is either asking for me to make a program that compares each digit in both floating point numbers up the the second decimal place number and output if they match or not....or they are asking me to round off both numbers to the 2nd decimal number and then compare the numbers to see if they match.

However the examples they give don't make sense to me.
2.0 and 1.99998 would only be the same up to two decimal places if it is rounded up...but when you look at the second example; 2.0 and 1.98999, they have an output of "they are different" but if i round up in this case, they should be the same.

am i missing something or interpreting the question wrong? does it have anything to do with C++ rounding/not rounding up without a special library?
Last edited on Jun 9, 2020 at 1:47pm
Jun 9, 2020 at 2:33pm
2.0 is 2.00 to two decimal places
1.99998 is 2.00 to two decimal places.
So, to two decimal places these numbers are the same (2.00)


2.0 is 2.00 to two decimal places
1.98999 is 1.99 to two decimal places.
So, to two decimal places, these numbers are different (2.00 and 1.99)
Jun 9, 2020 at 3:04pm
don't overthink it.
its probably something simple like this that they want:
if(abs(x-y) < 0.001) //the constant may be off, figure out what you need, my head is foggy today.
Topic archived. No new replies allowed.