Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJames Thomas
6,452 Points8*7+8%5+6/2 to me this == 59.6. Apparently I am wrong.
Apparently I am wrong. Please explain?
5 Answers
Steven Parker
231,269 PointsYou're forgetting about operator precedence.
All multiplicative operations (which include multiplication, division, and modulus) are performed before any additive opertions ( + or - ).
I'll bet you can figure it out now without a spoiler.
Dane Parchment
Treehouse Moderator 11,077 PointsOk so for Computer Science the Order of Operations is:
- P ---> Parentheses
- E ---> Exponents
- M ---> Multiplication
- D ---> Division (This is for programming only, division holds the same weight as multiplication
- M ---> Modulos (This is for programming only, and it can has the same order weight as division and multiplication)
- A ---> Addition
- S ---> Subtraction
So by following these rules we know that our addition will be done last and our multiplication, division, and Modulos will be done first, so this could have effectively been written as (8*7) + (8%5) + (6/2)
We know that 8 * 7 = 56
We know that 8 % 5 = 3
We know that 6 / 2 = 3
So when we add them all together we get 56 + 3 + 3 ----- or ----- 56 + 6 = 62
Steven Parker
231,269 PointsThere's no common rule for all of "computer science", operator precedence varies with language. For example, in C#, multiplication does not take precedence over division — so 12/2*3 is 18, not 2.
See the reference page I linked in the title of my answer for more information.
Dane Parchment
Treehouse Moderator 11,077 PointsThat is because of associativity, so you are correct there! I realized that I have not specified that division and modulos hold the same weight as multiplication so I will fix that.
Steven Parker
231,269 PointsBy the C# rules, the associativity of multiplicative operations is left-to right. That's why 12/2*3 evaluates to 18, instead of 2.
I recommend following the rules of the language you are programming in. If there are "general rules" that the language is "breaking", it doesn't seem like they would be useful to the developer and they could lead to confusion and unexpected program behavior.
Dane Parchment
Treehouse Moderator 11,077 PointsI see where you are coming from, I have a math background so I always see that there are general rules (keeps us sane) but in programming I have noticed that some languages seem to break this (for some reason or another).
Chris Freeman
Treehouse Moderator 68,454 PointsPEMDMAS does apply in Python where it's more like PE[MDM][AS]
>>> 12 / 2 * 3
18.0 # not 2
>>> 12 / 4 % 2 * 3
3.0 # / then % then *
>>> 25 % 13 / 2 * 3
18.0 # % then / then *
>>> 15 - 3 + 2
14 # not 10
Justin Coberly
16,929 PointsNormally the % (modulo) operator has the same level of precedence as multiplication and division. So in this statement we can evaluate it from left to right.
8*7 = 56 8%5 = 3 <- the remainder is the answer to a modulus expression 6/2 = 3 so... 56+3+3 = 62
James Thomas
6,452 PointsExcellent answers, thanks all. Sometimes learning the write software it's difficult to see the wood for the trees but this has been explained well!
Umut İnanç
Courses Plus Student 818 PointsI like to put paranteses between numbers which are going to be multiplied, divided or modulated to seperate additive and multiplicative operations to make it easier:
(8*7)+(8%5)+(6/2)
This way you can easily solve it.
(56)+(3)+(3) = 62
Justin Coberly
16,929 PointsJustin Coberly
16,929 PointsNormally the % (modulo) operator has the same level of precedence as multiplication and division. So in this statement we can evaluate it from left to right.
8*7 = 56 8%5 = 3 <- the remainder is the answer to a modulus expression 6/2 = 3 56+3+3 = 62