consider the following program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int i;
long int rr;
double dd;
double arr[10] = {55.550,55.551,55.552,55.553,55.554,55.555,55.556,55.557,55.558,55.559};
printf("\n\nTest1");
for(i=0;i<10;i++)
{
dd = 100 * arr[i];
rr = (long int)dd;
printf("\n 100 * %10.4lf == %10.4lf >>>>> %ld",arr[i], dd, rr);
}
printf("\n\nTest2");
for(i=0;i<10;i++)
{
printf("\n 100 * %10.4lf == %10.4lf >>>>> %ld",
arr[i], 100 * arr[i], (long int)(100 * arr[i]));
}
return 0;
}
After I execute this thing I got:
Test1
100 * 55.5500 == 5555.0000 >>>>> 5555
100 * 55.5510 == 5555.1000 >>>>> 5555
100 * 55.5520 == 5555.2000 >>>>> 5555
100 * 55.5530 == 5555.3000 >>>>> 5555
100 * 55.5540 == 5555.4000 >>>>> 5555
100 * 55.5550 == 5555.5000 >>>>> 5555
100 * 55.5560 == 5555.6000 >>>>> 5555
100 * 55.5570 == 5555.7000 >>>>> 5555
100 * 55.5580 == 5555.8000 >>>>> 5555
100 * 55.5590 == 5555.9000 >>>>> 5555
Test2
100 * 55.5500 == 5555.0000 >>>>> 5554 <-- Look at here !
100 * 55.5510 == 5555.1000 >>>>> 5555
100 * 55.5520 == 5555.2000 >>>>> 5555
100 * 55.5530 == 5555.3000 >>>>> 5555
100 * 55.5540 == 5555.4000 >>>>> 5555
100 * 55.5550 == 5555.5000 >>>>> 5555
100 * 55.5560 == 5555.6000 >>>>> 5555
100 * 55.5570 == 5555.7000 >>>>> 5555
100 * 55.5580 == 5555.8000 >>>>> 5555
100 * 55.5590 == 5555.9000 >>>>> 5555
Process returned 0 (0x0) execution time : -0.000 s
Press any key to continue.
Seems like when you multiply like this and then cast 55.550 the last digit is missed. Is that normal ?
How the casting should be preceded in that case ?