Mathematical Operation (PEMDAS AND BODMAS)

1k Views Asked by At

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.

2

There are 2 best solutions below

4
On BEST ANSWER

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.

0
On

PEDMAS/PEMDAS and BOMDAS/BODMAS are often taught wrongly.

(by the way, orders=exponents. Also, unary minus comes in priority after exponent, so PEUDMAS / BOUMDAS). So -5^2 is -(5^2) and is not (-5)^2

A way BODMAS/BOMDAS is often taught wrongly is saying O="of", when it means "orders", as in order of powers, as in, exponents.

Another way PEDMAS and BOMDAS are often taught wrongly, is PEDMAS is often taught as division before multiplication. And BOMDAS is often taught as multiplication before division. Similarly, PEMDAS and BOMDAS are often taught as addition before subtraction. When infact multiplication and division are equal priority. Also, addition and subtraction are equal priority.

If you have A-B+C+D+E you'd get a different result if you did addition first A-(B+C+D+E).

So 1-2+3 is different depending on if you do 1-(2+3)=-4 vs (1-2)+3=2

The correct answer is addition and subtraction have equal priority and are done left to right.

So saying that addition is done before subtraction can give wrong answers.

Calculators get it right.

Addition is commutative, you can swap things around.. e.g. 3+2 = 2+3

Subtraction is not.

Addition is associative, you can put brackets in different places and get the same result 1+2+3=1+(2+3)=(1+2)+3

Subtraction is not. 1-2-3 != 1-(2-3)

A similar issue occurs with multiplication and division

24/4*3

the correct answer is from left to right. so (24/4)*3 = 18

If a person is taught to do multiplication first, so, BOMDAS or PEMDAS taught wrongly. Then they'd do 24/(4*3)= 24/12=2 which is not the correct answer.

PEDMSA / BODMSA would probably always work though.. even if taught wrongly i.e. even if taught to do those in order. And not taught about the equal priority left to right rule for M/D, and for A/S. can you disprove this rule PEDMSA?-(division before multiplication, subtraction before addition)

As for modulus, a good way to think about it is to first consider a function Q, that takes two parameters, and gives a result a set of two numbers the like Q(9,2)=(4,1). Modulus just returns the second one so 9%2=1 and modulus is infix rather than prefix or postfix.

Q(75,4)=(18,3). So, 75%4=3.

A question related to yours is covered nicely here https://stackoverflow.com/questions/41377471/python-maths-100-25-3-4

Multiplication and priority are equal priority and equal priority to %. And all those are higher priority than subtraction.

 100 - ((25 * 3) % 4)
= 100 - (75 % 4)
= 100 - 3
= 97