Possible Duplicate:
Why does Visual Studio 2008 tell me .9 - .8999999999999995 = 0.00000000000000055511151231257827?
Why do simple doubles like 1.82 end up being 1.819999999645634565360?
I get a really weird behaviour while I compile and run the following program:
#include <iostream>
#define DIV 10000ll
long long gcd(long long a, long long b) {
if(b==0) return a;
else return gcd(b, a % b);
}
int main() {
int t;
std::cin >> t;
while(t--) {
double n1;
std::cin >> n1;
long long inum=(long long)(n1*DIV);
std::cout << inum << std::endl;
if(inum==0) { std::cout << 1 << std::endl; }
else std::cout << DIV/gcd(inum,DIV) << std::endl;
}
return 0;
}
When I enter as input:
1
0.0006
I get as output
5
2000
That means: (long long)0.0006*10000
is equal to 5 and not to 6. Why is this happening?