Given this class definition:
package {
public class Calc {
public static function I(i:Number, s:Number):Number {
var A:Number = helper_A(s);
var B:Number = helper_B(s);
var C:Number = helper_C(s);
return A * i + (Math.exp(B * i + C) - Math.exp(C) / B);
}
}
}
Static functions helper_A, helper_ B and helper_C do some math operations (One-liners with basic calculation and Math.pow()).
When calling this with
trace(Calc.I(27, 1985));
How is it possible that the following difference in brackets placement in the return statement of function I has an effect on the calculated outcome?
This version 1)
return A * i + (Math.exp(B * i + C) - Math.exp(C) / B);
traces 0.01721552341268775
While this version 2)
return A * i + ((Math.exp(B * i + C) - Math.exp(C)) / B);
traces 0.017092065919602526
From what I can tell, the result should be the same, mathematically. How come that Actionscript/Flash calculates the erroneous result without the brackets? Is there some weird internal caching, order, memory, foobar, … thing going on here?
(Math.exp(B * i + C) - Math.exp(C) / B);Without parentheses
Math.exp(C) / Bis executed first as division/has higher precedence than subtraction-.((Math.exp(B * i + C) - Math.exp(C)) / B);Here
(Math.exp(B * i + C) - Math.exp(C))is executed first due to the parentheses, and then the result is divided by B. This is clearly different than the first version.This is not anything related to AS3. This is well defined mathematical rule. Division, multiplication has higher precedence than addition, subtraction and parentheses is required to do addition, subtraction before multiplication, division.