Why height*height != height**2. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why height*height != height**2.

in BMI question🙋 if I find BMI using weight/height*height it's not passing all test cases. But if I use weight/height**2 to find BMI it passed all test cases. I want to know reason behind it. Or it's just malfunctioning 😊 Note: height is in floating value.

7th Feb 2021, 3:43 PM
Mohd Aadil
Mohd Aadil - avatar
3 Answers
+ 10
** operator has higher precedence over / operator. On the other hand, * operator shares equal precedence with / operator. weight / height * height Is processed from left to right because * and / operator shares the same precedence. weight / height ** 2 `height ** 2` is processed first. The result is then used as RHS operand for division operation, using <weight> as the LHS operand. https://www.tutorialspoint.com/python3/operators_precedence_example.htm
7th Feb 2021, 4:00 PM
Ipang
+ 4
They are different because: Weight/height*height == (Weight/height)*height That's mean that first divide and then multiply. To solve the question you need the following: Weight/(height*height) == Weight/height**2 That's mean that first multiply and then divide. I hope this helps you to understand what's happening in the operation.
7th Feb 2021, 4:01 PM
CLAD
CLAD - avatar
+ 3
I appreciate your work CLAD and Ipang . You guys are rock 🤘
7th Feb 2021, 4:05 PM
Mohd Aadil
Mohd Aadil - avatar