The fact is that what you think is not there in a double
For eg. 1/3 = 0.33333333.... but in a language with base 3, it is 0.1
Similarly, as a computer can only store data like 2s, 0.1 would be 0.000(1100) recurring in base 2.
As a computer, can't such data, it stores an approximate value.
Hence, if you think a double stores 2.345, it might actually be storing 2.3450000000000002.
But there is a way which will only work for small no. of decimal places
1 2 3 4 5 6 7 8 9 10
|
#include<iostream>
using namespace std;
void main()
{
double d;
cout<<"Enter a no. ";
cin>>d;
while(float(d)!=float(double(int(d))))d*=10;
cout<<d<<endl;
}
|
The logic is that you keep multiplying with 10 until the number's decimal part is 0, i.e., integral part==number