+ 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.
4 Antworten
+ 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
+ 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.