2

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());
    }
Nejat
  • 31,784
  • 12
  • 106
  • 138
french13
  • 75
  • 1
  • 7
  • 1
    or it has to do with how the debugger formats the variables – ratchet freak Jan 06 '15 at 16:05
  • Try using `std::vector` and compare the results to `QVector`. In theory, these data structures only copy variables, they don't affect the content of their data. – Thomas Matthews Jan 06 '15 at 16:09
  • I've thought about that, but every value is formatted as a double. I would think that means they're all treated the same, right? – french13 Jan 06 '15 at 16:22

1 Answers1

3

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.

Nejat
  • 31,784
  • 12
  • 106
  • 138