Search code examples
c++qttruncationqvectorqdebug

QVector Append truncating/rounding values


I am using QVector.append to append a double to QVector. Using my debugger, I can see that the double has a value of 40.783333499999998, however, the QVector values are rounded to 40.7833. Again, I see this on my debugger, not using qDebug so I'm relatively sure that the error is in the storage/appending function and not some other loss of precision elsewhere in my code. It's very important that the vector contains the full value. My code is below. In the debugger I can see that both the DictionaryOfValues and the iterator contains full precision, but the dataMatrix value is rounded. I can't seem to find where this error occurs. I'm using QT 5.2.1 MinGW 32 bit. Any help would be greatly appreciated.

In this code, DictionaryOfValues is a QMap and dataMatrix is a QMap>

    QMapIterator<QString,double> i(DictionaryOfValues);
    while (i.hasNext())
    {
        i.next();
        dataMatrix[i.key()].append(i.value());
    }

Solution

  • Actually that's because of the way debugger formats QVector values. The Actual value in the vector is not rounded at all. You can test it by checking the output of qDebug using this simple example :

    double a = 40.783333499999998;
    
    QVector<double> vec;
    vec.append(a);
    
    qDebug() <<"double :"<<qSetRealNumberPrecision(17) <<a;
    qDebug() <<"Vector: "<< qSetRealNumberPrecision(17) << vec;
    

    You can see that that the two outputs are the same :

    double : 40.783333499999998
    Vector: QVector(40.783333499999998)

    The default precision value is 6. You can change it by qSetRealNumberPrecision when using qDebug.