hello i have this simple code but it doesn't seem to work. i input 523.21563 but in the end it looks like the program has 523.21563720703125 stored which is obiously not correct could someone tell me how this comes .
my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int main()
{
float nmbr=523.21563;
float temp=nmbr-floor(nmbr);
int count=0;
while ((temp*10-floor(temp*10))!=0) {
cout<<temp<<endl;
temp=(temp*10-floor(temp*10));
count++;
}
cout<<count<<endl;
system("pause");
return 0;
}
try using double instead of float. If you still have problems, then it's a limitation of computer science in general as a 32bit/64bit number will have finite precision.
So it looks like the float stores the number as:
0.21563720703125
The double stores it as:
0.215630003285094862803816795349
However, note that all of the extra decimal places are not an indication of precision. Since data is stored as a binary number, and binary numbers will not round nicely into bases of 10, the extra digits represent the minimum that can be represented by the datatype.