I'm using floats for these operations:
Which of these two is more precise?
- (a * b) / c
or
- (a / c) * b
Does it matter at all or does it depend on the situation? If so, which should I choose in what cases?
See Question&Answers more detail:osI'm using floats for these operations:
Which of these two is more precise?
or
Does it matter at all or does it depend on the situation? If so, which should I choose in what cases?
See Question&Answers more detail:osReally, if you don't use double then you are misguided, and you don't care about precision.
Otherwise, you get the best error bounds if the first result is slightly lower than the next higher power of two. For example, calculating (pi * e) / sqrt (2), you get the best error bounds by calculating (e / sqrt (2)) * pi, because e / sqrt (2) ≈ 1.922 is close below 2. Results close to the next higher power of two have a lower relative error.
For addition and subtraction of a large number of items, it's best to first subtract items of equal magnitude and opposite sign (x - y is calculated exactly if y/2 ≤ x ≤ 2y), and otherwise combining numbers giving the smallest possible results.