3

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?

  • 3
    I believe you have found a bug, yes. – Adrian Larson Feb 16 '17 at 14:31
  • 1
    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? – Kasper Feb 16 '17 at 15:14
1

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: Exponent Operator

|improve this answer|||||
1

Quote from the Trailhead Advanced Formulas module

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

|improve this answer|||||
  • 1
    That doesn't explain why the observed value differs from the expectation. – Adrian Larson Feb 16 '17 at 14:26
  • I suppose that in Salesforce formulas are evaluated from left to right, but the sentence is not clear I agree – benahm Feb 16 '17 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 :) – benahm Feb 16 '17 at 14:30
1

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
|improve this answer|||||
New contributor
Darren Janeczek is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.