What will be the answer for x for the following x = 27 * 24 \ 4 /2
is it 81 or 324 - considering that there is an integer division(/) which is said to have lower precedence than (/,*)
Your help in this regard will be much appreciated
What will be the answer for x for the following x = 27 * 24 \ 4 /2
is it 81 or 324 - considering that there is an integer division(/) which is said to have lower precedence than (/,*)
Your help in this regard will be much appreciated
Interpreting '\' as division:
Usually multiplication and division have the same precedence, just like addition and subtraction, and are thought as left associative. Hence your operation gives $$ x = \left[(27*24)/4\right]/2 = 81 $$
If by '\' you mean "integer division", i.e. $a$ \ $b=\lfloor a/b\rfloor$, then probably it's precedence seems to depend on the programming language. In Python $(//)$ has the same precedence as $(*)$ and $(/)$. On the other hand in C++ $(/)$ is polymorphic (it's output depends on the type of the input values), while in Visual Basic it seems to have a lower precedence, indeed. In that case you would get $$ x= \left(27*24\right) \text{ \ } (4/2)=324 $$
7 / 5. There is no "integer divided by integer gives real", you have to write for example (double) 7 / 5. Same precedence as *, associates to the left. Best bet, language independent: If you aren't 100% clear, use parentesis. Maybe you get it right, but it helps unconfuse whoever has to read your code later (probably youself in a week or so ;-). Parentesis are cheap; finding mysterious bugs due to misreading expressions is long drawn out, embarrasing, and expensive.
– vonbrand
Mar 25 '13 at 17:16