Welcome to the world of the floating point approximation. In double precision floating point 0.6 - 0.5 != 0.2 - 0.1 because only 0.5 has an exact representation in binary floating point. If you look at the decimal approximation of the difference to some small number of digits you will see them as equal when their underlying binary representation is not. Have a read here
For example:
QList<double> oldList;
QList<double> newList;
oldList << 0.0 << 0.1 << 0.2 << 0.31 << 0.5 << 0.6 << 0.76;
for (int i = 1; i < oldList.size(); ++i) {
double value = oldList.at(i) - oldList.at(i-1);
qDebug
() <<
QString("Computed %1 - %2 = %3") .arg(oldList.at(i), 22, 'f', 20)
.arg(oldList.at(i-1), 22, 'f', 20)
.arg(value, 22, 'f', 20);
if (!newList.contains(value)) {
newList << value;
qDebug() << "Inserted";
}
}
QList<double> oldList;
QList<double> newList;
oldList << 0.0 << 0.1 << 0.2 << 0.31 << 0.5 << 0.6 << 0.76;
for (int i = 1; i < oldList.size(); ++i) {
double value = oldList.at(i) - oldList.at(i-1);
qDebug() << QString("Computed %1 - %2 = %3")
.arg(oldList.at(i), 22, 'f', 20)
.arg(oldList.at(i-1), 22, 'f', 20)
.arg(value, 22, 'f', 20);
if (!newList.contains(value)) {
newList << value;
qDebug() << "Inserted";
}
}
To copy to clipboard, switch view to plain text mode
outputs:
"Computed 0.10000000000000000555 - 0.00000000000000000000 = 0.10000000000000000555"
Inserted
"Computed 0.20000000000000001110 - 0.10000000000000000555 = 0.10000000000000000555"
"Computed 0.30999999999999999778 - 0.20000000000000001110 = 0.10999999999999998668"
Inserted
"Computed 0.50000000000000000000 - 0.30999999999999999778 = 0.19000000000000000222"
Inserted
"Computed 0.59999999999999997780 - 0.50000000000000000000 = 0.09999999999999997780"
Inserted
"Computed 0.76000000000000000888 - 0.59999999999999997780 = 0.16000000000000003109"
Inserted
(0.1, 0.11, 0.19, 0.1, 0.16)
"Computed 0.10000000000000000555 - 0.00000000000000000000 = 0.10000000000000000555"
Inserted
"Computed 0.20000000000000001110 - 0.10000000000000000555 = 0.10000000000000000555"
"Computed 0.30999999999999999778 - 0.20000000000000001110 = 0.10999999999999998668"
Inserted
"Computed 0.50000000000000000000 - 0.30999999999999999778 = 0.19000000000000000222"
Inserted
"Computed 0.59999999999999997780 - 0.50000000000000000000 = 0.09999999999999997780"
Inserted
"Computed 0.76000000000000000888 - 0.59999999999999997780 = 0.16000000000000003109"
Inserted
(0.1, 0.11, 0.19, 0.1, 0.16)
To copy to clipboard, switch view to plain text mode
Bookmarks