Different return for different formulas but same value [duplicate]












-4
















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";
}









share|improve this 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. 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
















-4
















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";
}









share|improve this 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. 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














-4












-4








-4









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";
}









share|improve this question

















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++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 18:11









Swordfish

9,15711335




9,15711335










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. 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

















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












1 Answer
1






active

oldest

votes


















2














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.






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    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.






    share|improve this answer




























      2














      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.






      share|improve this answer


























        2












        2








        2







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 18:08









        Arshia001Arshia001

        725510




        725510















            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)