Search code examples
c++cvisual-c++visual-studio-debugging

Visual C++ values differ while printing and debugging?


I am checking a piece of code. Everything is correct but concept I am not able to understand.

double a = 0.001;
double b = 0.001;
double c = a * b;

printf ("%lf", c);

While debugging in visual c++ when i am pointing mouse over c after 3rd line it is displaying 9.999999999999995e-007 but while printing it is showing correct result i.e. 0.000001. I want to know actually what value it displays in debug tooltip and how it represents and converts.


Solution

  • This is the result of rounding performed by printf.

    The printf format %lf rounds to a default precision. From the top of my head, I think the default is 6, that is why you get 0.000001.

    The debugger shows the actual content of the double. Due to the nature of floating point arithmetic, the result of 0.001 * 0.001 is not actually 0.000001, but an approximation although with very small difference.

    By using other formats, you can see the difference. E.g. try printf("%.15e", c);