Results 1 to 3 of 3

Thread: How to get rid of duplicated double values from QList?

  1. #1
    Join Date
    Sep 2011
    Posts
    86
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default How to get rid of duplicated double values from QList?

    Want to create a list of double values but whatever i do seems not to work, every time i get list with duplicated values on it.

    Code below should create a list with all intervals between values from oldList, oldList is ascending and contains duplicated values.
    Qt Code:
    1. QList<double> newList;
    2.  
    3. for(int i = 1; i < oldList.count(); ++i)
    4. {
    5. for(int j = 0; j < oldList.count() - i; ++j)
    6. {
    7. value = oldList.at(j + i) - oldList.at(j);
    8. if(!newList.contains(value))
    9. {
    10. newList << value;
    11. }
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    Is this code correct?

    ...probably it's something stupid but can't see any error here.
    Last edited by code_err; 22nd February 2012 at 18:01.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to get rid of duplicated double values from QList?

    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:
    Qt Code:
    1. QList<double> oldList;
    2. QList<double> newList;
    3.  
    4. oldList << 0.0 << 0.1 << 0.2 << 0.31 << 0.5 << 0.6 << 0.76;
    5.  
    6. for (int i = 1; i < oldList.size(); ++i) {
    7. double value = oldList.at(i) - oldList.at(i-1);
    8. qDebug() << QString("Computed %1 - %2 = %3")
    9. .arg(oldList.at(i), 22, 'f', 20)
    10. .arg(oldList.at(i-1), 22, 'f', 20)
    11. .arg(value, 22, 'f', 20);
    12. if (!newList.contains(value)) {
    13. newList << value;
    14. qDebug() << "Inserted";
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    outputs:
    Qt Code:
    1. "Computed 0.10000000000000000555 - 0.00000000000000000000 = 0.10000000000000000555"
    2. Inserted
    3. "Computed 0.20000000000000001110 - 0.10000000000000000555 = 0.10000000000000000555"
    4. "Computed 0.30999999999999999778 - 0.20000000000000001110 = 0.10999999999999998668"
    5. Inserted
    6. "Computed 0.50000000000000000000 - 0.30999999999999999778 = 0.19000000000000000222"
    7. Inserted
    8. "Computed 0.59999999999999997780 - 0.50000000000000000000 = 0.09999999999999997780"
    9. Inserted
    10. "Computed 0.76000000000000000888 - 0.59999999999999997780 = 0.16000000000000003109"
    11. Inserted
    12. (0.1, 0.11, 0.19, 0.1, 0.16)
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to ChrisW67 for this useful post:

    code_err (23rd February 2012)

  4. #3
    Join Date
    Sep 2011
    Posts
    86
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to get rid of duplicated double values from QList?

    I would never figure it out. Magical world of floating point you say. So I know now what i am fighting with.

Similar Threads

  1. Chancing values to a QList<object>
    By terhje in forum Newbie
    Replies: 2
    Last Post: 19th May 2011, 12:01
  2. Program not finding an existing double in a QList<double>
    By aarelovich in forum Qt Programming
    Replies: 2
    Last Post: 9th May 2011, 20:59
  3. Replies: 4
    Last Post: 20th August 2010, 13:54
  4. Progress Bar with double values
    By ^NyAw^ in forum Qt Programming
    Replies: 8
    Last Post: 23rd March 2007, 15:37
  5. Values not being appended in a QList
    By Kapil in forum Newbie
    Replies: 5
    Last Post: 21st April 2006, 10:20

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.