Q:

printing float value to console precision problem_

I have just noticed something weird in my code
I have created simple app to show you what I'm talking about
float Test1 = 112.12;
float Test2 = 0.12;

printf("Test1 %15f", Test1);
printf("Test1 %15f", Test1);

I have always thought that if I write Test1 = 112.12 it means it's equal 112.120000000
however I get 112.120003
and on the second example I get 0.11999 or 0.120000 it depends
Could someone explain me why is this happening and what happens if I try to divide these two numbers will they get converted to double (or any other more precise data type) so I can get higher precision or they will be used in their current form (float)
I have used VS 2010 to test this (if it matters)

A:

Two observations:

computers use base 2, not base 10
floating-point numbers are not precise

Because of the first point, there are some numbers that don't translate well from our system to the computer's system.  0.3 can't be 100% precisely represented directly in binary (that's not to say that computers can't represent it precisely, just that they can't with the commonly used system).
Because of the second point, it's always dangerous to use floating-point numbers for exact values.  I would recommend reading the Wikipedia article on floating point numbers -- it mentions accuracy problems.

