Reputation: 11
I was working through Bjarne Stroustrup's "Programming-Principles And Practice Using C++" book and I'm currently stumped by the 5th "Drill" question of Chapter 4.
There is a series of questions with the purpose of building a complex If-Else If-Else statement that I think I understand the logic behind. Yet, this 5th question has stumped me: "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 whatever reason, I can't get the "If statement" to correctly evaluate between floating-point values outside of 0.5. Or, in other words, it will only correctly evaluate to the output listed above if the difference of the two values is equal to 0.5.
Here is my code:
int main() {
double i = 0;
double j = 0;
while (std::cin >> i >> j) {
std::cout << "\n";
std::cout << i << "\t" << j << "\n" << "\n";
if (i - j == 0.01) {
std::cout << "The Two Numbers Are Almost Equal" << "\n";
}
else if (i > j) {
std::cout << "The Larger Value Is " << i << "\n";
}
else if (i == j) {
std::cout << "The Numbers Are Equal" << "\n";
}
else {
std::cout << "The Larger Value Is " << j << "\n";
}
}
}
I even tried just making a basic if-else statement with the same premise, and the compiler still would not evaluate the correct statement in regard to the first condition. Any help?
Upvotes: -1
Views: 128
Reputation: 66449
Read all the words in
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
again.
It says that if the numbers aren't equal, you first write out which is the larger and smaller, and after that, you also check if they are close and print another message if they are.
So you're going to print either one or two messages.
It also says that the difference should be less than 0.01, not exactly 0.01, for the numbers to be "close".
Since the difference between equal numbers is definitely less than 0.01, you need to separate this into "equal" and "not equal" cases.
That is,
if (i == j)
{
}
else
{
// The numbers are not equal.
if (i > j)
{
}
else
{
// j > i must be the case since the numbers are not equal.
}
// Now add the "close" message, if appropriate.
if (std::abs(i - j) < 0.01)
{
}
}
Upvotes: 2
Reputation: 320
That's because the way floating-point numbers are compared. Instead of checking for exact equality (i - j == 0.01)
, compare their absolute difference to a threshold. Change:
if (i - j == 0.01)
To:
if (std::abs(i - j) < 0.01)
Also avoid doing equality checks with floating point numbers if you can. Move this
else if (i == j) {
std::cout << "The Numbers Are Equal" << "\n";
to the else
block:
if (std::abs(i - j) < 0.01) {
std::cout << "The Two Numbers Are Almost Equal\n";
} else if (i > j) {
std::cout << "The Larger Value Is " << i << '\n';
} else if (i < j) {
std::cout << "The Larger Value Is " << j << '\n';
} else { // if i is not larger or less than j, it is equal to j
std::cout << "The Numbers Are Equal\n";
}
Upvotes: 1