# Order of Operations

The order of operations is typically represented by PEMDAS.

    (P)arenthesis
    (E)xponents
    (M)ultiplication and (D)ivision.
    (A)ddition and (S)ubtraction.

But PEMDAS leaves out quite a bit. So I have outlined the precedence of how everything is evaluated in CalcPad.

0. (P)arenthesis
1. Functions (in function notation)
2. Unary Right: Factorial '!'
3. (E)xponentation & Root '√'
4. Unary Left: Negation, Not, Logical Not, Bitwise Not/Complement (note: also right associativity)
5. Implicit Multiplication (note: only has higher precedence if enabled in the settings)
6. (M)utliplication, (D)ivision, Modulo, Of
7. (A)ddition, (S)ubtraction, On, Off
8. Bitwise Shift ('<<' and '>>') and Rotate
9. Relational ('>', '<', '<=', and '>=')
10. Equality ('==' and '!=')
11. And, Nand
12. Xor
13. Or, Nor

As you can see there are a ton more things to think about when deciding the order of operations. See: C# operator precedence here.

## Math Convention

The order of operations is so well standardized these days that people forget or do not know, that many of these rules we follow are not mathematical fact, but math convention. In the past it was not unheard of for a mathematican to declare the convention he was using at the top of a paper.

### Implicit Multiplication

Implicit multiplication is something that can be confusing at times. People argue both ways and when I was involved in making the decision it seemed to be split down the middle 50/50. It was often taught that implicit multiplication has a higher precedence than explicit multiplication. This convention seems to have fallen almost completely out of use as of the time of this writing. Both Google Calculator and WolframAlpha do not use this convention. You can turn this on in the settings though and 2x/2x will always equal 1, no matter what x is.

### Division and Units

The order of operations outlines above does not completely apply to the use of units. I did this on purpose to make dealing with units more intuitive. For example, implicit multiplication always has higher precedence when dealing with units, even if the option is not turned on. However, that means the statement "1/16 inch" is not the same as "(1/16) inch". Instead it is the same as 1/(16 inch) since the value of units are treated more like a whole. You can further tweak the behavior of CalcPad in the settings to give division between number higher precedence.

There may be other reasons why you would select this option as well, as it treats division as a numerator and denominator by default. Like implicit multiplication, this is also something that is argued about. Some believe multiplication and division should have the same priority, while others maintain it should be treated like a numerator and denominator.

### Negation Operator

The negation operator can be confusing at times as well, although I haven't seen arguments over it before. We're told that a negative multiplied by a negative is a positive. So you may expect -22 to be positive. However, the "-" symbol in this contex is not a suffix, it is not a negative sign that denoates that the 2 is a negative number. Rather, it is a positive 2 with a negation operator in front of it. Not only that, but the negation operator has lower precedence than the exponentation operator. This means that -22 is equivalent to -(22) and so the result is a positive 2. The result is positive unless you write it as (-2)2.

In the settings you can choose to give the negation operator higher priority than the exponentation operator, so that -2 is essentially treated as a negative number instead of a positive number that is eventually negated. In this case -22 would produce a positive number.