Printf() in C

Mar 2, 2011 at 1:36pm
I code like this:

printf("Input: ");
scanf("%lf",&n);
printf("6.2: %6.2lf\n",n);
printf("14.10: %14.10lf\n",n);
printf("20.16: %20.16lf\n",n);

The type of n is double. My problem is the output is not the same as input. For example, n = 1.999, I understand the output of the first and second one. But the last one, %20.16lf, I don't know why the output is like this: 1.9990000000000001. Why we have the number 1 here? And when n = 1.9999, the result is very strange.
Can you explain?
Thank you very much :)
Mar 2, 2011 at 2:06pm
Floating point roundoff error.

Floating point representation in IEEE format is not exact.
Mar 2, 2011 at 2:41pm
Thank you. I understand now :)
Topic archived. No new replies allowed.