2

Possible Duplicate:
C++ double precision and rounding off

Code:

int main(void)
{
    double a = 12;
    double b = 0.5;
    double c = 0.1;

    std::cout.precision(25);
    std::cout << a << std::endl;
    std::cout << b << std::endl;
    std::cout << c << std::endl;
    std::cout << a + b << std::endl;
    std::cout << a + c << std::endl;

    return 0;
}

Output:

12
0.5
0.1000000000000000055511151
12.5
12.09999999999999964472863

Why does GCC represent 0.1 and 0.5 differently? When adding, they are represented differently. It seems 0.5 and whole numbers a represented differently that other floats. Or is this just something going on in the io library? What causes this behavior?

Community
  • 1
  • 1
d-_-b
  • 6,555
  • 5
  • 40
  • 58
  • [related](http://stackoverflow.com/questions/5098558/float-vs-double-precision). – iammilind May 25 '12 at 06:45
  • 1
    Doubles don't have infinite precision. The values you're assigning are not representable exactly using doubles. – MatthewD May 25 '12 at 06:46
  • I've clarified my question. It's not about precision. – d-_-b May 25 '12 at 06:50
  • @toor I suspect you will find the answer to why certain numbers give a better representation than others in "What every computer scientist should know..." article as well. – Lundin May 25 '12 at 06:58
  • 1
    it is. 0.1 is for floating point encoding the same thing as 1/3 for decimal encoding. It's an infinite series of digits. You only have 53 binary digits (as far as I remember) which boils down to around 18 decimal digits. Please read the paper presented below. It helps. – Tobias Langner May 25 '12 at 07:00
  • 1
    @iammilind: The "C++ double precision" is not a duplicate. That asker wants to control the rounding, while this one is asking why he's not getting the exact answer. That said I am sure I've seen questions answered with link to "What every computer scientist should know about floating-point arithmetic" and those would probably be duplicates. – Jan Hudec May 25 '12 at 07:53

4 Answers4

5

Just as decimal numbers with a finite number of digits can only exactly represent numbers that are a sum of powers of 10, binary floating point numbers can only exactly represent numbers that are a sum of powers of 2.

In this case, 0.1 cannot be represented as a finite sum of powers of 2, whereas 0.5 and 12 can (0.5 is equal to 2-1 and 12 is equal to 23 + 22).

As a further example, 0.75 can also be exactly represented in binary floating point, because it can be expressed as 2-1 + 2-2.

caf
  • 233,326
  • 40
  • 323
  • 462
4

0.1 can not be exactly represented in binary as it is not a power of 2 and must be represented by a number that is really, really, really close instead. This is why students are taught never to use the == operator when comparing floating-point numbers and banking applications almost always store money as two integers, the dollar amount and the number of pennies.

Casey
  • 10,297
  • 11
  • 59
  • 88
  • Of course. But that leads me to perhaps my answer. Is 0.5 stored differently? This was really my question. Sometimes code is not enough to express your amusement. – d-_-b May 25 '12 at 06:52
4

the default answer for these questions:

What every computer scientist should know about floating-point arithmetic

basically, it's the inaccuracy of the floating point encoding. You have around 17 significant digits and doing arithmetic operations reduces them.

wovano
  • 4,543
  • 5
  • 22
  • 49
Tobias Langner
  • 10,634
  • 6
  • 46
  • 76
0

read this

http://floating-point-gui.de/

triclosan
  • 5,578
  • 6
  • 26
  • 50