infinite loop on floating point number
The following code is used to get the number of decimal places after the decimal point, but can go into an infinite loop sometimes on my compiler:
g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48)
The questions are
1) why does a double like 134.29 triggers an infinite loop? some floating point numbers cause such infinite loop, but most other floating point numbers just work fine. Are there any rules of thumb on the numbers which cause the infinite loop?
2) baring using string length to get the number of decimal places, is there any other way to improve the existing code to avoid triggering the infinite loop?
double v = data;
int8_t dpcount = 0;
static double EPS=1E-3;
while(v - (int)v > EPS)
v *= 10;
std::cout<< "data: " << data << " v: "<< v << " dpcount: " << (int) dpcount << std::endl;