Consider the following code:
$result *= $oldFactor / $newFactor; //using shorthand *= operator
which is actually this:
$result = $oldFactor / $newFactor * $result; //division done first
I can also manually write it as this:
$result = $result * $oldFactor / $newFactor; //multiplication done first
I am under impression that multiplication is a simpler operation and it does not suffer as much from rounding errors, compared to divide operation.
Also, I have this feeling that for most day-to-day human numbers, multiply operation will produce a "larger number" before it is divided (assuming numbers being used are often greater than 1). And larger numbers after being divided are more numerically stable. Example .. consider 5 * 7 / 2.3
where first operation (mult) is precise, as those numbers are represented exactly in binary. Then division is done and it's as precise as we are going to get. But consider 7 / 2.3 * 5
, where first operation is divide, and already we produce a number that cannot be represented exactly in binary, and the next operation (mult) exaggerates any imprecision via multiplication.
My question is basically ... does this matter? Do I indeed lose precision when using divide first, or am I perfectly safe to use whichever ordering of operations that looks best for me and I will be getting the same result?