Reputation: 3966
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.
Upvotes: 0
Views: 324
Reputation: 1776
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);
Upvotes: 3
Reputation: 6505
There are different implementations for conversion from double/float to strings and base on that you will get different results. The mos accurate implementation i know is std::sringstream
and i know that is different from printf
at least on IOS devices.
The visual studio interface as far as i know is coded in C# so you might want to look into how C# converts doubles to strings to get the answer you are looking for.
Razvan.
Upvotes: 0