1
$\begingroup$

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.

$\endgroup$
6
  • $\begingroup$ "In PEMDAS we are doing multiplication first" This is incorrect. See en.wikipedia.org/wiki/Order_of_operations#Mnemonics $\endgroup$
    – JRN
    Apr 29, 2020 at 10:15
  • 3
    $\begingroup$ Universal solution: Use parentheses to tell the program exactly what you want to calculate. $\endgroup$
    – Matti P.
    Apr 29, 2020 at 10:15
  • 1
    $\begingroup$ In case of doubt, always use parenthesis. The program will do exactly what you want and you won't have to worry more about order rules. $\endgroup$ Apr 29, 2020 at 10:17
  • 1
    $\begingroup$ I think that PEMDAS, BODMAS, etc are more trouble than they are worth. Maths and computing can differ in this area. Note that ÷ and / rarely used in serious maths, Division is usually written as $\frac{a+b}{c+d}$ which implies the sequence without parentheses. % is never used for modulus, that is indicated quite differently. So, this is actually a computing question and more specifically a Python one; Not all languages agree on these matters. $\endgroup$
    – badjohn
    Apr 29, 2020 at 11:30
  • 1
    $\begingroup$ @barlop Yes the long line implies parentheses. My point was only that ÷ and / are rarely used and that not so many parentheses are required in a typical maths style. Just a day or two ago, I read in the retro-computing exchange that Algol allows reassigning operator priority. $\endgroup$
    – badjohn
    Apr 28, 2022 at 22:30

2 Answers 2

1
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ That's it, the order of the multiplications and divisions don't change te final result, because multiplications are conmutative and you can see $\frac{a}{b}$ as the multiplication $a\frac{1}{b}$. $\endgroup$ Apr 29, 2020 at 10:28
  • $\begingroup$ Ok thanks I didn't thought like that. Silly me $\endgroup$
    – Pragyan
    Apr 29, 2020 at 10:32
  • $\begingroup$ Also, you might want to check this question and comments/answers therein: math.stackexchange.com/questions/767595/… $\endgroup$
    – ModCon
    Apr 29, 2020 at 10:34
  • 2
    $\begingroup$ @AlejandroBergasaAlonso Make that the multiplication $a\cdot\dfrac1b$ (or $a\times\dfrac1b$ if the multiplication-dot wasn't clear). Notation like $a\dfrac1b$ isn't used in maths, and would be downright misleading with numerals --- $3\dfrac17$ does not mean $\dfrac37$. $\endgroup$
    – Rosie F
    Apr 29, 2020 at 16:24
0
$\begingroup$

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
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .