0
quint64 a = 11;
double b = 0.1;
double v = a / b;
quint64 v1 = a / b;
quint64 v2 = v;
quint64 v3 = (double)((double)a / (double)b);
qDebug() << QObject::tr("v = %1, v1 = %2, v2 = %3, v3 = %4").arg(v).arg(v1).arg(v2).arg(v3);

I tried the code in Qt creator as pasted, which use mingw compiler. The result is as following:

"v = 110, v1 = 109, v2 = 110, v3 = 109"

My question is, why v1 and v3 are both 109? How the compiler deal with such a sentence 'a / b'? And how can I get result v3 as 110 in one line?

f0g
  • 1
  • 2
  • 1
    This has nothing to do with Qt. The floating point representation of `0.1` is slightly less than `0.1`. There is no way to represent `0.1` exactly in binary floating point, and thus you get those results. Try with something that can be represented exactly, e.g. `16` and `0.125` - you'll get exact results. – Kuba hasn't forgotten Monica May 14 '18 at 06:32
  • Thanks. That is to say, the conversion must cause precision lost, right? and I still don't understand why v3 get different value with v2 ... – f0g May 14 '18 at 08:10

0 Answers0