0

Possible Duplicate:
Sum of float is not exact

So i have the code:

#include <iostream>
using std::cout;
using std::endl;

int main () {
   float x = 0.0001;
   float y = 0;
   for (int i=0; i < 10000; i++) {
      y += x;
   }
   cout << y << endl;
   return 0;
}

I'm expecting the output to be 1, but I'm getting 1.00005, how come? What would i need to change to get "1" keeping the code more or less the same?

Community
  • 1
  • 1
Thatdude1
  • 905
  • 5
  • 18
  • 31

2 Answers2

2

A binary float can't represent every possible decimal rational number exactly. When it can't, round-off errors will occur. You're seeing a build up of these round-off errors.

WeirdlyCheezy
  • 704
  • 4
  • 4
1
int main () {
   int x = 1;
   int y = 0;
   for (int i=0; i < 10000; i++) {
      y += x;
   }
   cout << y/10000.0 << endl;
   return 0;
}

Integers are exact, floating point numbers often aren't. If you need exactness the easiest way without using a decimal library is to keep the math in the integer domain until you need it.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Integers are not actually exact. We just tend to use them more often for things that are exact, such as address arithmetic. Other arithmetic is not only inexact but typically worse with integers than with floating-point, such as division or interest calculations. – Eric Postpischil Oct 15 '12 at 20:45
  • @EricPostpischil, you make a good point for operations that naturally produce a non-integer result such as division or the exponentiation that goes into interest calculations. But I think there's still merit in my point, as seen in the example. – Mark Ransom Oct 15 '12 at 20:52
  • @EricPostpischil I thought most integer implementations of common operations (add,sub,mul,div) are exact *if* both the inputs *and* the true output of the operation are within the range that implementation supports (and for division, if you avoid dividing by zero and if the true output is an integer). I would hope the C++ standard makes this mandatory for int. Am I missing something? – WeirdlyCheezy Oct 16 '12 at 00:12
  • @WeirdlyCheezy: Division is the same for integer and floating point: If the mathematical result is representable, the actual result is exact. If not, there is a rounding error (toward zero for integer arithmetic, selectable for floating point). In floating point, if the mathematical result of addition, subtraction, or multiplication is representable, the actual result is exact. If the result is not representable, you get an approximation (or infinity if appropriate). This improves on integer arithmetic, which fails in various ways when the result is not representable. – Eric Postpischil Oct 16 '12 at 00:55
  • @EricPostpischil I agree that once you have a result that isn't exactly representable, both integers and floats have their appropximation-related shortfalls. However, it is sometimes possible to write code that does "exact integer arithmetic", ie, operates under the assumption that all results will be representable (and even to enforce that assumption). For floats, enforcing/assuming exact-representability of the result is generally impractical so well-written FP code takes it as a given that the output is an approximation. If you have any counter examples, please feel free to share. – WeirdlyCheezy Oct 16 '12 at 04:58