Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ieee-754

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?

like image 650
Roger Dahl Avatar asked Jan 07 '15 23:01

Roger Dahl


People also ask

Is floating-point multiplication faster than division?

Multiplication is faster than division.

How do you find the precision of a floating-point?

The precision of a floating-point number is determined by the mantissa. For a 32 bit floating-point DSP, the mantissa is generally 24 bits. So the precision offered by a 32 bit DSP with a mantissa of 24 bits is at least that of a 24 bit fixed-point device.

Is division slower than multiplication Java?

Division is much slower, than multiplication.


2 Answers

It depends on the magnitude of the values. Obviously if one divides by zero, all bets are off, but if a multiplication or division results in a denormal subsequent operations can lose precision.

You may find it useful to study Goldberg's seminal paper What Every Computer Scientist Should Know About Floating-Point Arithmetic which will explain things far better than any answer you're likely to receive here. (Goldberg was one of the original authors of IEEE-754.)

like image 126
Edward Avatar answered Oct 16 '22 07:10

Edward


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).

like image 44
vinc17 Avatar answered Oct 16 '22 06:10

vinc17