Wednesday, 14 October 2015

Order of Operations

Brackets ( and ) are used for grouping expressions and changing the order of operation. This calculator evaluates the expression following the rules of BODMAS (Wikipedia link).

The specific order of operation for TLCalc is defined as below:
  1. Brackets: (, [ 
  2. Negate (-) 
  3. Angle (@), factorial (!) and Exponential (E)
  4. Power (^)
  5. Permutation (nPr) and combination (nCr)
  6. Multiplication (*), division (/) and modulus (%)
  7. Addition (+) and subtraction (-) (or negate if first term)
  8. Comparators (>, >=, <, <=)
  9. Equality (==, !=)
  10. AND (&&)
  11. OR (||), XOR (|^) and IMPLIES (=>)
Items with higher priority are evaluated first with items on the same level being evaluated from left to right. E.g. 2^3nPr2%3 = ((2^3)nPr2)%3 .

Tip: Always use brackets if in doubt.

WARNING: The order of operation was drastically changed in V1.18.0. The changes were made to make TLCalc more closely reflect the order of operations used in other calculators and programming languages such as C and Java. Note that in all versions, BODMAS is always adhered to.

Technical extra for those that are curious:
You may notice that the minus sign (-) is located in 2 places in the order of operations list. The parse will parse the '-' sign with lowest priority first. For example:
  -2^2 is interpreted as -(2^2) because negate has a lower priority (of 7) than power (4) while
  --2^2 is interpreted as -((-2)^2) since the first minus is parsed under priority 7 (like before) and the second is parsed under priority 2. The second minus is applied only to the 2 because the second minus has higher priority than power so the second minus is applied before the power and the power is applied before the first minus.   Confusing? Use brackets and you won't be.