I'm trying to convert a double with 4 decimals in a quint32, but when I iterate the list, the values are different.
I added a breakpoint at the first cycle and these are the variables, how can I make "i" to be 112778?
EDIT:
This is the code:
QList<double> list;
list << 11.2778;
list << 11.3467;
list << 11.3926;
list << 11.4531;
list << 11.4451;
list << 11.4625;
list << 11.4579;
list << 11.4375;
list << 11.4167;
list << 11.6285;
list << 11.5625;
list << 11.4427;
list << 11.4278;
list << 11.4063;
list << 11.2500;
for(double value : list)
{
double v = value * 10000;
quint32 i = v;
qDebug() << v << i;
}
I was expecting the numbers to be converted to quint32 without floating point, but that's not the result
CodePudding user response:
This is just a question of floating point precision in C , and there are a lot of existing SO questions on the topic. The problem I think arises from the fact that: 11.2778 * 10000
might not get calculated to be exactly 112778
. It might think it is 112777.999999999
, or whatever. Converting to an int doesn't round to the nearest integer, it just truncates everything after the decimal point. So that's how you can end up with 112777
. To fix this, you can simply force it to round:
for(double value : list)
{
double v = value * 10000;
quint32 i = qRound(v); // Round the double to get the best int
qDebug() << value << v << i;
}
CodePudding user response:
I printed value, too, as like below.
qDebug() << value << v << i;
The output is below.
11.2778 112778 112777
11.3467 113467 113467
11.3926 113926 113926
11.4531 114531 114530
11.4451 114451 114451
11.4625 114625 114625
11.4579 114579 114579
11.4375 114375 114375
11.4167 114167 114167
11.6285 116285 116285
11.5625 115625 115625
11.4427 114427 114427
11.4278 114278 114278
11.4063 114063 114063
11.25 112500 112500
Do you mean that the last digit is different? If so, the last digit may be different because decimal numbers are not hold on the memory digit by digit.