Different return for different formulas but same value [duplicate]
This question already has an answer here:
Is floating point math broken?
28 answers
Tl;dr: Why does C++ see a difference between x/y*y and x/(y*y)?
I was doing some tasks at CodeWars and had to calculate variable and assign the result (whole exercise at the bottom):
bmi = weight / height ^ 2
if bmi <= 30.0 return "Overweight"
test values: weight = 86.7, height = 1.7
The formula that I used is
double bmi = weight / (height*height)
and for if-statement later it is
else if (bmi > 25 && bmi <= 30)
return "Overweight";
else
return "Obese";
and test value calculated with formula is equal to 30. But when I run the code, my return was Obese. I wrote line to print out the result of calculation, it showed 30. But when I changed formula to double bmi = weight / height / height
, the correct value is returned. Therefore, the problem is in the formula, but why is C++ see a difference between x/y/y and x/(y*y), even though it prints out the same result?
Given exercise:
Write function bmi that calculates body mass index (bmi = weight / height ^ 2).
if bmi <= 18.5 return "Underweight"
if bmi <= 25.0 return "Normal"
if bmi <= 30.0 return "Overweight"
if bmi > 30 return "Obese
My code:
#include <iostream>
#include <string>
// I created two functions to use two formulas for BMI
std::string bmi(double, double);
std::string bmi2(double, double);
int main()
{
std::cout << bmi(86.7, 1.7) << std::endl << std::endl; // Calling the function
std::cout << bmi2(86.7, 1.7);
}
std::string bmi(double w, double h)
{
double bmi = w/(h*h); // Formula for BMI
std::cout <<"Calculated with w/(h*h): "<< bmi << std::endl;
if (bmi <= 18.5)
return "Underweight";
else if(bmi>18.5 && bmi <= 25.0)
return "Normal";
else if (bmi>25 && bmi <= 30)
return "Overweight"; // It should return this
else
return "Obese"; // But goes with that
}
std::string bmi2(double w, double h)
{
double bmi = w/h/h; // Formula for BMI
std::cout <<"Calculated with w/h/h: "<< bmi << std::endl;
if (bmi <= 18.5)
return "Underweight";
else if(bmi>18.5 && bmi <= 25.0)
return "Normal";
else if (bmi > 25 && bmi <= 30)
return "Overweight";
else
return "Obese";
}
c++
marked as duplicate by Neil Butterworth, Matthieu Brucher, S.M., Swordfish, gsamaras Nov 24 '18 at 18:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Is floating point math broken?
28 answers
Tl;dr: Why does C++ see a difference between x/y*y and x/(y*y)?
I was doing some tasks at CodeWars and had to calculate variable and assign the result (whole exercise at the bottom):
bmi = weight / height ^ 2
if bmi <= 30.0 return "Overweight"
test values: weight = 86.7, height = 1.7
The formula that I used is
double bmi = weight / (height*height)
and for if-statement later it is
else if (bmi > 25 && bmi <= 30)
return "Overweight";
else
return "Obese";
and test value calculated with formula is equal to 30. But when I run the code, my return was Obese. I wrote line to print out the result of calculation, it showed 30. But when I changed formula to double bmi = weight / height / height
, the correct value is returned. Therefore, the problem is in the formula, but why is C++ see a difference between x/y/y and x/(y*y), even though it prints out the same result?
Given exercise:
Write function bmi that calculates body mass index (bmi = weight / height ^ 2).
if bmi <= 18.5 return "Underweight"
if bmi <= 25.0 return "Normal"
if bmi <= 30.0 return "Overweight"
if bmi > 30 return "Obese
My code:
#include <iostream>
#include <string>
// I created two functions to use two formulas for BMI
std::string bmi(double, double);
std::string bmi2(double, double);
int main()
{
std::cout << bmi(86.7, 1.7) << std::endl << std::endl; // Calling the function
std::cout << bmi2(86.7, 1.7);
}
std::string bmi(double w, double h)
{
double bmi = w/(h*h); // Formula for BMI
std::cout <<"Calculated with w/(h*h): "<< bmi << std::endl;
if (bmi <= 18.5)
return "Underweight";
else if(bmi>18.5 && bmi <= 25.0)
return "Normal";
else if (bmi>25 && bmi <= 30)
return "Overweight"; // It should return this
else
return "Obese"; // But goes with that
}
std::string bmi2(double w, double h)
{
double bmi = w/h/h; // Formula for BMI
std::cout <<"Calculated with w/h/h: "<< bmi << std::endl;
if (bmi <= 18.5)
return "Underweight";
else if(bmi>18.5 && bmi <= 25.0)
return "Normal";
else if (bmi > 25 && bmi <= 30)
return "Overweight";
else
return "Obese";
}
c++
marked as duplicate by Neil Butterworth, Matthieu Brucher, S.M., Swordfish, gsamaras Nov 24 '18 at 18:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Just in passing, the>
tests aren't needed. Ifbmi <= 18.5
theelse if
won't be reached. It just needs to test forbmi <= 25.0
.
– Pete Becker
Nov 24 '18 at 18:09
add a comment |
This question already has an answer here:
Is floating point math broken?
28 answers
Tl;dr: Why does C++ see a difference between x/y*y and x/(y*y)?
I was doing some tasks at CodeWars and had to calculate variable and assign the result (whole exercise at the bottom):
bmi = weight / height ^ 2
if bmi <= 30.0 return "Overweight"
test values: weight = 86.7, height = 1.7
The formula that I used is
double bmi = weight / (height*height)
and for if-statement later it is
else if (bmi > 25 && bmi <= 30)
return "Overweight";
else
return "Obese";
and test value calculated with formula is equal to 30. But when I run the code, my return was Obese. I wrote line to print out the result of calculation, it showed 30. But when I changed formula to double bmi = weight / height / height
, the correct value is returned. Therefore, the problem is in the formula, but why is C++ see a difference between x/y/y and x/(y*y), even though it prints out the same result?
Given exercise:
Write function bmi that calculates body mass index (bmi = weight / height ^ 2).
if bmi <= 18.5 return "Underweight"
if bmi <= 25.0 return "Normal"
if bmi <= 30.0 return "Overweight"
if bmi > 30 return "Obese
My code:
#include <iostream>
#include <string>
// I created two functions to use two formulas for BMI
std::string bmi(double, double);
std::string bmi2(double, double);
int main()
{
std::cout << bmi(86.7, 1.7) << std::endl << std::endl; // Calling the function
std::cout << bmi2(86.7, 1.7);
}
std::string bmi(double w, double h)
{
double bmi = w/(h*h); // Formula for BMI
std::cout <<"Calculated with w/(h*h): "<< bmi << std::endl;
if (bmi <= 18.5)
return "Underweight";
else if(bmi>18.5 && bmi <= 25.0)
return "Normal";
else if (bmi>25 && bmi <= 30)
return "Overweight"; // It should return this
else
return "Obese"; // But goes with that
}
std::string bmi2(double w, double h)
{
double bmi = w/h/h; // Formula for BMI
std::cout <<"Calculated with w/h/h: "<< bmi << std::endl;
if (bmi <= 18.5)
return "Underweight";
else if(bmi>18.5 && bmi <= 25.0)
return "Normal";
else if (bmi > 25 && bmi <= 30)
return "Overweight";
else
return "Obese";
}
c++
This question already has an answer here:
Is floating point math broken?
28 answers
Tl;dr: Why does C++ see a difference between x/y*y and x/(y*y)?
I was doing some tasks at CodeWars and had to calculate variable and assign the result (whole exercise at the bottom):
bmi = weight / height ^ 2
if bmi <= 30.0 return "Overweight"
test values: weight = 86.7, height = 1.7
The formula that I used is
double bmi = weight / (height*height)
and for if-statement later it is
else if (bmi > 25 && bmi <= 30)
return "Overweight";
else
return "Obese";
and test value calculated with formula is equal to 30. But when I run the code, my return was Obese. I wrote line to print out the result of calculation, it showed 30. But when I changed formula to double bmi = weight / height / height
, the correct value is returned. Therefore, the problem is in the formula, but why is C++ see a difference between x/y/y and x/(y*y), even though it prints out the same result?
Given exercise:
Write function bmi that calculates body mass index (bmi = weight / height ^ 2).
if bmi <= 18.5 return "Underweight"
if bmi <= 25.0 return "Normal"
if bmi <= 30.0 return "Overweight"
if bmi > 30 return "Obese
My code:
#include <iostream>
#include <string>
// I created two functions to use two formulas for BMI
std::string bmi(double, double);
std::string bmi2(double, double);
int main()
{
std::cout << bmi(86.7, 1.7) << std::endl << std::endl; // Calling the function
std::cout << bmi2(86.7, 1.7);
}
std::string bmi(double w, double h)
{
double bmi = w/(h*h); // Formula for BMI
std::cout <<"Calculated with w/(h*h): "<< bmi << std::endl;
if (bmi <= 18.5)
return "Underweight";
else if(bmi>18.5 && bmi <= 25.0)
return "Normal";
else if (bmi>25 && bmi <= 30)
return "Overweight"; // It should return this
else
return "Obese"; // But goes with that
}
std::string bmi2(double w, double h)
{
double bmi = w/h/h; // Formula for BMI
std::cout <<"Calculated with w/h/h: "<< bmi << std::endl;
if (bmi <= 18.5)
return "Underweight";
else if(bmi>18.5 && bmi <= 25.0)
return "Normal";
else if (bmi > 25 && bmi <= 30)
return "Overweight";
else
return "Obese";
}
This question already has an answer here:
Is floating point math broken?
28 answers
c++
c++
edited Nov 24 '18 at 18:11
Swordfish
10.2k11436
10.2k11436
asked Nov 24 '18 at 17:52
Zan StanislawZan Stanislaw
104
104
marked as duplicate by Neil Butterworth, Matthieu Brucher, S.M., Swordfish, gsamaras Nov 24 '18 at 18:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Neil Butterworth, Matthieu Brucher, S.M., Swordfish, gsamaras Nov 24 '18 at 18:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Just in passing, the>
tests aren't needed. Ifbmi <= 18.5
theelse if
won't be reached. It just needs to test forbmi <= 25.0
.
– Pete Becker
Nov 24 '18 at 18:09
add a comment |
Just in passing, the>
tests aren't needed. Ifbmi <= 18.5
theelse if
won't be reached. It just needs to test forbmi <= 25.0
.
– Pete Becker
Nov 24 '18 at 18:09
Just in passing, the
>
tests aren't needed. If bmi <= 18.5
the else if
won't be reached. It just needs to test for bmi <= 25.0
.– Pete Becker
Nov 24 '18 at 18:09
Just in passing, the
>
tests aren't needed. If bmi <= 18.5
the else if
won't be reached. It just needs to test for bmi <= 25.0
.– Pete Becker
Nov 24 '18 at 18:09
add a comment |
1 Answer
1
active
oldest
votes
Because arithmetic operators are evaluated from left to right, and by inserting parentheses around y * y
you change the order of execution. The order matters because of floating point errors, on which you can find any number of articles around the internet. In short, integers can be represented precisely up to a certain number, but fractions will almost always be slightly inaccurate, which is why you should never test for exact equality of floats and doubles.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Because arithmetic operators are evaluated from left to right, and by inserting parentheses around y * y
you change the order of execution. The order matters because of floating point errors, on which you can find any number of articles around the internet. In short, integers can be represented precisely up to a certain number, but fractions will almost always be slightly inaccurate, which is why you should never test for exact equality of floats and doubles.
add a comment |
Because arithmetic operators are evaluated from left to right, and by inserting parentheses around y * y
you change the order of execution. The order matters because of floating point errors, on which you can find any number of articles around the internet. In short, integers can be represented precisely up to a certain number, but fractions will almost always be slightly inaccurate, which is why you should never test for exact equality of floats and doubles.
add a comment |
Because arithmetic operators are evaluated from left to right, and by inserting parentheses around y * y
you change the order of execution. The order matters because of floating point errors, on which you can find any number of articles around the internet. In short, integers can be represented precisely up to a certain number, but fractions will almost always be slightly inaccurate, which is why you should never test for exact equality of floats and doubles.
Because arithmetic operators are evaluated from left to right, and by inserting parentheses around y * y
you change the order of execution. The order matters because of floating point errors, on which you can find any number of articles around the internet. In short, integers can be represented precisely up to a certain number, but fractions will almost always be slightly inaccurate, which is why you should never test for exact equality of floats and doubles.
answered Nov 24 '18 at 18:08
Arshia001Arshia001
749611
749611
add a comment |
add a comment |
Just in passing, the
>
tests aren't needed. Ifbmi <= 18.5
theelse if
won't be reached. It just needs to test forbmi <= 25.0
.– Pete Becker
Nov 24 '18 at 18:09