Salesforce Asked by abdn on December 6, 2021
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?
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
Answered by Darren Janeczek on December 6, 2021
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
Answered by benahm on December 6, 2021
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);
Answered by Rohit Mourya on December 6, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP