高龙 wrote:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
float j = 1.0f, i =j;
int n = 0;
for(; i - j - 1.00f; ++n, j /= 10)
{
;
}
printf("%d", n);
return EXIT_SUCCESS;
}
I supposed the output was 6, because the float's precision is 6, but
as the result, the output is 20.
why?
Try running a version of your program like the following, and examine
your output (which will likely be different from mine). Think about
what it tells you. In particular, think about why j is never, for j < 1,
exactly ten to some negative power.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
int main(void)
{
float j = 1.0f, i = j;
int hexdigit, decdigit;
int n = 0;
hexdigit = (3 + CHAR_BIT * sizeof j) / 4;
decdigit = (2 + CHAR_BIT * sizeof j) * 0.301;
printf("With this implementation, sizeof(float) = %zu,\n"
"and FLT_DIG = %d. In spite of this, we will\n"
"use precisions of %d (hex) and %d (decimal).\n"
" i = %.*a (%.*g)\n\n",
sizeof(float), FLT_DIG, hexdigit, decdigit,
hexdigit, i, decdigit, i);
for (; i - j - 1.00f; ++n, j /= 10) {
printf("n = %d, j = %.*a (%.*g),\n"
" i - j - 1.f = %.*a (%.*g)\n",
n,
hexdigit, j, decdigit, j,
hexdigit, i - j - 1.f, decdigit, i - j - 1.f);
}
printf("%d\n", n); /* note the end-of-line character at
the end of the last line of output. */
return EXIT_SUCCESS;
}
With this implementation, sizeof(float) = 4,
and FLT_DIG = 6. In spite of this, we will
use precisions of 8 (hex) and 10 (decimal).
i = 0x8.00000000p-3 (1)
n = 0, j = 0x8.00000000p-3 (1),
i - j - 1.f = -0x8.00000000p-3 (-1)
n = 1, j = 0xc.ccccd000p-7 (0.1000000015),
i - j - 1.f = -0xc.cccd0000p-7 (-0.1000000238)
n = 2, j = 0xa.3d70a666p-10 (0.01000000015),
i - j - 1.f = -0xa.3d70a666p-10 (-0.01000000015)
n = 3, j = 0x8.3126e666p-13 (0.0009999999776),
i - j - 1.f = -0x8.3126e666p-13 (-0.0009999999776)
n = 4, j = 0xd.1b716666p-17 (9.999999311e-05),
i - j - 1.f = -0xd.1b716666p-17 (-9.999999311e-05)
n = 5, j = 0xa.7c5ab333p-20 (9.99999902e-06),
i - j - 1.f = -0xa.7c5ab333p-20 (-9.99999902e-06)
n = 6, j = 0x8.637bc000p-23 (9.999998838e-07),
i - j - 1.f = -0x8.637bc000p-23 (-9.999998838e-07)
n = 7, j = 0xd.6bf93333p-27 (9.999998838e-08),
i - j - 1.f = -0xd.6bf93333p-27 (-9.999998838e-08)
8