Operator Precedence in formula field

I have formula field like this:

`2*2^3`

Result = `64`

Expected Result = `16` - I am getting this result in excel

I corrected the formula by adding parenthesis.

My question is that exponentiation has higher precedence than multiplication and division in Math. Why Salesforce formula not following this rule? Is it a bug or intentional?

• I believe you have found a bug, yes. Commented Feb 16, 2017 at 14:31
• I find this a bit worisome. Could such a simple bug exist for so long without anyone noticing? Or have there been changes lately, and are there no unit tests to catch this? Commented Feb 16, 2017 at 15:14

UPDATE: This issue has been officially acknoledged as an issue. You can track the progress here: https://success.salesforce.com/issues_view?Id=a1p3A000001YpUV

Currently (early 2020), Salesforce formula operator precedence reverses (*,/) with (^). In normal applications, the exponents will be evaluated first, but in Salesforce formulas, the multiplication (or division) operators are evaluated before exponents.

Users must use parentheses to work around this non-standard interpretation of mathematical expressions.

I tried 6 different combinations of operator sequences on the same four numbers, and 4/6 failed. Here are the results of my test:

``````> ---------------------------------------------
> DISCREPANCY!!!!
>  * MathOperatorTest2__c
>    - Salesforce  [96549157373046880]
>    - Interpreted [9886633717]
>    - Formula is  [2 + 5 * 7 ^ 11]
> ---------------
Javascript> 2 + (5 * 7) ** 11
96549157373046880

Javascript> 2 + 5 * 7 ** 11
9886633717

> ---------------------------------------------
> DISCREPANCY!!!!
>  * MathOperatorTest3__c
>    - Salesforce  [10000011]
>    - Interpreted [156261]
>    - Formula is  [2 * 5 ^ 7 + 11]
> ---------------
Javascript> (2 * 5) ** 7 + 11
10000011

Javascript> 2 * 5 ** 7 + 11
156261

> ---------------------------------------------
> DISCREPANCY!!!!
>  * MathOperatorTest4__c
>    - Salesforce  [6.617444900424221e+53]
>    - Interpreted [859377]
>    - Formula is  [2 + 5 ^ 7 * 11]
> ---------------
Javascript> 2 + 5 ** (7 * 11)
6.617444900424221e+53

Javascript> 2 + 5 ** 7 * 11
859377

> ---------------------------------------------
> DISCREPANCY!!!!
>  * MathOperatorTest6__c
>    - Salesforce  [34359738379]
>    - Interpreted [235]
>    - Formula is  [2 ^ 5 * 7 + 11]
> ---------------
Javascript> 2 ** (5 * 7) + 11
34359738379

Javascript> 2 ** 5 * 7 + 11
235
``````
• Wow, we are in mid 2022 and it's still not fixed! Commented May 12, 2022 at 13:31

You can add (+), subtract (-), multiply (*), or divide (/) numerical values. You can also use exponentiation (^) in formulas. These operators work just like they do on a calculator. Formulas follow the order of operations, and you can group operations in parentheses.

As said in this quote, the formulas are evaluated as if they are wrote in a calculator, that means evaluated from left to right

Try your formula on this online calculator without any parentheses you'll get 16

• That doesn't explain why the observed value differs from the expectation. Commented Feb 16, 2017 at 14:26
• I suppose that in Salesforce formulas are evaluated from left to right, but the sentence is not clear I agree Commented Feb 16, 2017 at 14:27
• Try this online-calculator.com/scientific-calculator I did 2*2^3 I got 16 so they are right that works just in a calculator when not using parentheses :) Commented Feb 16, 2017 at 14:30

The definition of `^` is not of exponent in the apex guide. It is mentioned as "Bitwise exclusive OR operator. Exclusive ORs each bit in x with the corresponding bit in y so that the result bit is set to 1 if exactly one of the bits is set to 1 and the other bit is set to"

For doing exponent, you can use the pow function. Eg:

``````Decimal myDecimal = 4;
Decimal powDec = myDecimal.pow(2);
system.assertEquals(powDec, 16);
``````

Below is the snapshot of Exponent Operator from Apex Guide:

• And how would you do power functions in a formula? Commented Feb 16, 2017 at 13:35
• Again, that's `Apex`. OP Is working with a formula. Commented Feb 16, 2017 at 13:47
• And actually it looks like in formula functions `^` is the exponential operator. Commented Feb 16, 2017 at 13:51
• @AdrianLarson Yes we can say that, because it does that job of exponential in formula editor Commented Feb 16, 2017 at 13:53