Results 1 to 5 of 5

Thread: QVector.append rounding values

  1. #1
    Join Date
    Jun 2012
    Posts
    19
    Thanks
    3

    Default QVector.append rounding values

    i have a function that returns a double value (eg. 1.76896558753375)

    then i append the value to a QVector<double> with some_vector.append(value)

    after append, the value is 1.76897 in the qvector.

    why is this happening and how can it be prevented?

    stepping through qvector's append function:
    Qt Code:
    1. template <typename T>
    2. void QVector<T>::append(const T &t)
    3. {
    4. const T copy(t);
    5. const bool isTooSmall = uint(d->size + 1) > d->alloc;
    6. if (!isDetached() || isTooSmall) {
    7. QArrayData::AllocationOptions opt(isTooSmall ? QArrayData::Grow : QArrayData::Default);
    8. reallocData(d->size, isTooSmall ? d->size + 1 : d->alloc, opt);
    9. }
    10. if (QTypeInfo<T>::isComplex)
    11. new (d->end()) T(copy);
    12. else
    13. *d->end() = copy;
    14. ++d->size;
    15. }
    To copy to clipboard, switch view to plain text mode 

    both t and copy are the correct un-rounded value until ++d->size, after that line, the rounded value is appended instead of the full value.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QVector.append rounding values

    How do you test that ? If by printing it to qDebug, then it is not a reliable way
    Qt Code:
    1. #include <QVector>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char ** argv){
    5. const double test_value = 1.76896558753375;
    6. QVector<double> vec;
    7. vec.append(test_value);
    8. const double from_vec = vec.at(0);
    9. if (qFuzzyCompare(from_vec,test_value))
    10. qDebug() << "OK";
    11. else
    12. qDebug() << "ERROR!";
    13. qDebug() << vec;
    14. qDebug() << qSetRealNumberPrecision(15) << vec;
    15. return 0;
    16. }
    To copy to clipboard, switch view to plain text mode 
    output:
    OK
    QVector(1.76897)
    QVector(1.76896558753375)
    Tested with Qt 4.8, gcc 4.5.2 and 5.1.1, gcc 4.8, windows 8
    Can you run this example on your machine and post the output ?

  3. #3
    Join Date
    Jun 2012
    Posts
    19
    Thanks
    3

    Default Re: QVector.append rounding values

    you're correct, it does print the full value with qSetRealNumberPrecision! i was using the debugger to check the values. ok so the problem then is with comparison i guess..

    after the values are inserted into the vector, the vector is used with std::upper_bound and std::lower_bound to find the original value, and that's where the problem is. upper/lower bound aren't returning the position of the double in the qvector.

    Qt Code:
    1. //b = list.begin();
    2. //e = list.end();
    3. //n = list.size();
    4. double get_upper_bound(double x) const
    5. {
    6. QVector<double>::Iterator it = upper_bound(b,e,x);
    7. unsigned pos = it-b; // find the first element >= x. this is the issue, pos sometimes returns 0 instead of the proper location in the qvector
    8. return (pos/n);
    9. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QVector.append rounding values

    Can you post a minimal compilable example (sth. like in my post above) of std::upper_bound returning wrong position ?

  5. The following user says thank you to stampede for this useful post:

    splinterz (9th July 2014)

  6. #5
    Join Date
    Jun 2012
    Posts
    19
    Thanks
    3

    Default Re: QVector.append rounding values

    thanks for your patience stampede, but i've finally resolved this. the double that was passed to the original qvector for the upper_bound was passed to another function first... and that function had a float type as the parameter instead of a double. so the double->float->double conversion was altering the precision understandably.

Similar Threads

  1. QSpinBox : display values from a QVector
    By CTZStef in forum Qt Programming
    Replies: 4
    Last Post: 6th December 2013, 13:13
  2. Rounding in QT
    By TomASS in forum Newbie
    Replies: 2
    Last Post: 5th January 2012, 22:09
  3. Replies: 5
    Last Post: 2nd September 2011, 23:11
  4. Cannot append to QFile using QIODevice::Append
    By philwinder in forum Qt Programming
    Replies: 4
    Last Post: 17th November 2008, 09:09
  5. QVector - Append
    By krishbhala in forum Qt Programming
    Replies: 1
    Last Post: 20th December 2007, 10:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.