#include<stdio.h>
int main()
{
double fract=0;
int tmp;
//scanf("%lf",&fract);
fract=0.312;
printf("%lf",fract);
printf("\n\n");
while(fract>0){
fract*=(double)10;
printf("%d ",(int)fract);
fract-=(int)fract;
}
getch();
return 0;
}
this code shoud have an output of:
312
but somehing isn’t going right.. i’m using devcpp 4.9.9.2 compiler…
Kernighan & Plauger say, in their old but classic book “The Elements of Programming Style”, that:
They also say:
Both sayings point out that floating point arithmetic is not precise.
Note that some modern CPUs (IBM PPC) have IEEE 754:2008 decimal floating point arithmetic built-in. If using the correct types, then your calculation will be exact.