Results 1 to 3 of 3

Thread: Program not finding an existing double in a QList<double>

  1. #1
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Program not finding an existing double in a QList<double>

    This is the code that is giving me a hard time.

    The function is called four times, each changing only the value parameter

    Qt Code:
    1. bool ReservedWord::belongsToList(qreal start, qreal end, qreal step, qreal value){
    2. QList<qreal> list;
    3. list << start;
    4. while (list.last() <= end){
    5. list << list.last() + step;
    6. }
    7. list.removeLast();
    8.  
    9. qDebug() << "Checking for" << value << "on list" << list << "and the answer is" << list.contains(value);
    10. return list.contains(value);
    11. }
    To copy to clipboard, switch view to plain text mode 

    The program output is:

    Checking for 4.2 on list (4, 4.2, 4.4, 4.6, 4.8, 5, 5.2, 5.4) and the answer is true
    Checking for 4.4 on list (4, 4.2, 4.4, 4.6, 4.8, 5, 5.2, 5.4) and the answer is true
    Checking for 4.6 on list (4, 4.2, 4.4, 4.6, 4.8, 5, 5.2, 5.4) and the answer is false
    Checking for 5.2 on list (4, 4.2, 4.4, 4.6, 4.8, 5, 5.2, 5.4) and the answer is false

    The objective is to define a numbers list by defining it's beginning, it's end and a certain step. Then checking to see if a value belongs to the list. In two cases the value is clearly there but the it is not found.

    Any ideas of what is wrong?

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Program not finding an existing double in a QList<double>

    Double values suffer from machine precision. That means that 4.6 does not necessarily have to be exactly equal to 4.6. You should use another approach using qFuzzyCompare(), that takes into account this issue. Maybe you can write a template specialization for it, but that would mean you have that behavior throughout your program, and requires you to do template magic.

    Qt Code:
    1. bool SomeClass::listContains(qreal value) const
    2. {
    3. foreach (qreal v, theList) {
    4. if (qFuzzyCompare(value, v))
    5. return true;
    6. }
    7. return false;
    8. }
    To copy to clipboard, switch view to plain text mode 
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Program not finding an existing double in a QList<double>

    Yeah, I figured the machine precision thing. What I did not know is how to solve it. Since this part doesn't have to be efficient at all, I solved it by turning Everything to strings and comparing then. But I'll take the qFuzzyCompare into account.

    Thank you very much.

Similar Threads

  1. Double slider
    By whitefurrows in forum Qt Programming
    Replies: 8
    Last Post: 23rd February 2011, 15:29
  2. Qwt without double
    By tora in forum Qwt
    Replies: 1
    Last Post: 5th September 2009, 08:43
  3. Replies: 2
    Last Post: 24th June 2009, 15:38
  4. finding QWidgets in QList
    By babu198649 in forum Newbie
    Replies: 1
    Last Post: 26th July 2008, 11:58
  5. Replies: 1
    Last Post: 17th May 2006, 00:23

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.