0

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?

ZeroZ30o
  • 375
  • 2
  • 18
  • 3
    If you care about precision, why not use doubles? –  Aug 05 '17 at 16:27
  • I care about precision, but not THAT much. Since I'm forced to use one of the two, I would like to know which is better. – ZeroZ30o Aug 05 '17 at 16:30
  • 1
    I guess it depends on the values. If `a * b` results in an overflow (an infinity), it will be less precise. OTOH, if `a / c` results in an underflow (a denormal, or 0.0f), that will be less precise. Also, `(1 / 3) * 3` is not as accurate as `(1 * 3) / 3`. So if there is no chance for an overflow, I would use `a * b / c`. – Rudy Velthuis Aug 05 '17 at 16:41
  • In the absence of underflow and overflow in intermediate computation, either expression should be evaluated with error <= 1.5 ulp when using IEEE-754 single-precision operations with round-to-nearest-or-even mode. I don't know how to prove the bound, though. – njuffa Aug 05 '17 at 16:46
  • for fixed point multiply then divide, for float though there is no better way within the same precision, think about it... – old_timer Aug 06 '17 at 15:47
  • Possible duplicate of [Order of commutative mathematical operations](https://stackoverflow.com/q/49506802/608639), [C/C++ Math Order of Operation](https://stackoverflow.com/q/11296854/608639), [What are the rules governing C++ single and double precision mixed calculations?](https://stackoverflow.com/q/4239770/608639), [Order of operations to maximize precision](https://stackoverflow.com/q/45524072/608639), etc. – jww Mar 27 '18 at 07:48
  • @jww so my topic is a duplicate of itself? Nice. And no, it isn't a duplicate because this one talks about precision, for a very specific operation. – ZeroZ30o Mar 29 '18 at 18:24

1 Answers1

5

Really, 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.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • To clarify, I care first about speed and then about precision, so double is not an option. – ZeroZ30o Aug 05 '17 at 19:02
  • Why do you think floats will be faster than doubles? See https://stackoverflow.com/questions/4584637/double-or-float-which-is-faster and numerous other posts for why this probably is not the case. –  Aug 05 '17 at 22:03
  • Simply because there is more data - which fills the CPU cache quicker, as stated in the post you have linked. – ZeroZ30o Aug 06 '17 at 15:50