Search code examples
ieee-754

IEEE-754 floating point: Divide first or multiply first for best precision?


What's better if I want to preserve as much precision as possible in a calculation with IEEE-754 floating point values:

a = b * c / d

or

a = b / d * c

Is there a difference? If there is, does it depend on the magnitudes of the input values? And, if magnitude matters, how is the best ordering determined when general magnitudes of the values are known?


Solution

  • Assuming that none of the operations would yield an overflow or an underflow, and your input values have uniformly distributed significands, then this is equivalent. Well, I suppose that to have a rigorous proof, one should do an exhaustive test (probably not possible in practice for double precision since there are 2^156 inputs), but if there is a difference in the average error, then it is tiny. I could try in low precisions with Sipe.

    In any case, in the absence of overflow/underflow, only the exact values of the significands matter, not the exponents.

    However if the result a is added to (or subtracted from) another expression and not reused, then starting with the division may be more interesting since you can group the multiplication with the following addition by using a FMA (thus with a single rounding).