I am learning Python and came across PEMDAS.
Python uses PEMDAS to solve mathematical equations.
But in lower classes like 5th or 6th we were taught BODMAS. I got confused and then made an equation to check which method gives me the correct answer.
My equation was
100-2⁵×8÷2+4
Now both PEMDAS and BODMAS gave me same result -24.
So how's that possible. In PEMDAS we are doing multiplication first and in BODMAS we are doing division first.
I actually got confused because when we enter
print 100-25*3%4
Python gives result 97 using PEMDAS. But if I use BODMAS then we get 25 .
PS:
In above python script * means multiplication and % means modulus i.e. if we write X%Y then we speak it as 'X divided by Y with J remaining'. The result of % is the J part (or remainder ) of division.
Tell me where I am doing wrong.
I am posting it here because I think it's more of a mathematical doubt then a python problem.
In both PEMDAS and BODMAS, there is no particular preference for multiplication or division, either can be done first and answer will be same. In general, an expression like $\frac{abcd}{ghij}$ can be evaluated by multiplying or dividing number in any order whatsoever and you will get the same result.
For example, check your expression, $2^5 \times 8 \div 2$ gives the same number regardless of the order in which you calculate. Moreover, if the two operations are far apart in an expression and separated by addition and subtraction operations in between, it is obvious that you will end up with same numbers whether you multiply first or divide.