Strange behaviour with double numbers
-
Hi All,
For some reason the QString::toDouble method does not return the right double value of a string. So I decided to write my own toDouble method.. I am trying to get a double number with a 17 digit precision.
When I run my code, the behaviour is different when I have a qDebug() statement in between.
I am posting the code below
...
int precision = 0;
int factor_max = 17;
if (num != (int) num) {
int factor = 1;
bool done = false;
double curnum = num;
while (!done) {
curnum = curnum * 10;
if (curnum == (int)curnum) {
qDebug() << "Loop curnum value " << curnum << endl;
precision = factor;
done = true;
}
else {
qDebug() << "Loop curnum value " << curnum << endl;
factor++;
if (factor == factor_max) {
precision = factor_max;
done = true;
}
}
}
}
...If I uncomment the qDebug statements, then the code yields correct results if not I get incorrect results..
I tested it with the following
"-46.2" returns as -46.2 with the qDebug statements in place
and comes out as 46.20000000000000284 if I comment the qDebug statements out.. I am puzzled about this.. Does anyone have any idea?
Thanks in advance for your time,
-