4

Does performing operations (like multiplication, division, addition, and subtraction) on floats and doubles maintain their degree of precision?

For example, if I multiplied (or divided, added, subtracted) 1000 floats together, would I still maintain 7 digits of precision?

I have read on this website here that the precision is maintained (http://floating-point-gui.de/formats/fp/), but I wanted to double check.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
M. Pope
  • 411
  • 6
  • 16

2 Answers2

6

There's precision and there's accuracy. The precision of a float is always the same. But the accuracy of your calculations depends on a variety of implementation-specific details.

For example, if each calculation introduces a half-LSB of error, then after 1000 calculations the result could be off by 500 LSBs. Hence, although the answer would be precise to 7 digits, it would only be accurate to 4 digits.

user3386109
  • 34,287
  • 7
  • 49
  • 68
  • Thanks! This is what I needed! – M. Pope May 07 '16 at 03:29
  • 4
    "In the worse case, each calculation will introduce a half-LSB of error, so after 1000 calculations, your result could be off by 500 LSBs." - that's quite a misleading statement to make, considering factors like how "1 LSB" isn't the same amount in every calculation. For example, `1e20 + 14 - 1e20` is generally accurate to 0 digits, despite only involving two operations' worth of rounding error. – user2357112 May 07 '16 at 04:14
  • 1
    @user2357112 Fair enough, I have reworded that paragraph. The problem of small differences of large numbers is indeed an issue, but it's slightly different than the problem of accumulated small errors that I was trying to elucidate. – user3386109 May 07 '16 at 04:44
  • 2
    Worst case error is one thing, density of probability of error another. For case of multiplication see http://stackoverflow.com/questions/12047410/the-number-of-correct-decimal-digits-in-a-product-of-doubles-with-a-large-number/12052359#12052359 – aka.nice May 07 '16 at 20:12
1

The answer depends on what you mean by "maintaining precision". A single floating point always has the same "precision" of about 7 digits (it's not exactly 7 digits due to binary storage).

Some calculations may introduce rounding error, which can make the least significant bit incorrect, but those errors can add up (like user3386109 explained in their answer) or they can be amplified. An example of an amplification would be if I was calculating a calculus limit of the form (f(x+h)-f(x))/h as h goes to zero. If f(x+0.0000001) should be 3.1234567, but I get 3.1234566 and f(x) gives the correct 3.1234568. Now, the formula should be (3.1234567-3.1234568)/0.0000001, which is -1, but I got (3.1234566-3.1234568)/0.0000001, which is -2.

Suddenly, my least significant digit is my most significant digit. There are other ways to amplify rounding errors and techniques for avoiding it.

Always be aware of rounding error when dealing with non-integer types. Some examples of rounding error failures

Kyle A
  • 928
  • 7
  • 17