Results 1 to 5 of 5

Thread: Wrong converting QVariant to double

  1. #1
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb Wrong converting QVariant to double

    Hi,

    I have to get a value from a database, for example, 2.3000. Must compare this value with the result of this calculation, as code:

    Qt Code:
    1. ...
    2. double y = 2.3;
    3. double x = qry.value(0).toDouble(); // Take 2.30000 in the database, this example;
    4.  
    5. qDebug() << y << x; // Ok, show 2.3 value for x and y;
    6.  
    7. double z = x - y; // It's result in -2.66454e-15!
    8.  
    9. if (z < 0.00) {
    10. ...
    11. }
    12. else
    13. {
    14. ...
    15. }
    To copy to clipboard, switch view to plain text mode 

    If I make a calculation explicitly declare the values for the variables x and y, result is correct.
    I created a method to convert a QString to a double that is working (code below). Any hint to resolve this as I use this function to work around the problem?

    Qt Code:
    1. ...
    2. double y = 2.3;
    3. QString sx = qry.value(0).toString();
    4.  
    5. double x = myfunction->toDouble(sx);
    6. double z = x - y; // It's OK, result 0
    7. ...
    To copy to clipboard, switch view to plain text mode 

    Thanks,

    Marcelo Estanislau Geyer

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Wrong converting QVariant to double

    Never compare a double to a constant, especially if the double is a result of some calculations. Rely on a range instead.
    Qt Code:
    1. double x = 2.3-q.value(0).toDouble();
    2. if(qAbs(x)<=0.001){
    3. // assume x==0
    4. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation Re: Wrong converting QVariant to double

    Ok, for calculations in general, do not constantly have problems? For example:

    Qt Code:
    1. ...
    2. double x = q.value(0).toDouble(); // Value is 2.3
    3. double y = 2.0;
    4. double z = x - z; // 0.3 "forever" ?
    To copy to clipboard, switch view to plain text mode 

    Thanks,

    Marcelo Estanislau Geyer

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Wrong converting QVariant to double

    Double type can't represent any possible value, it uses approximations for the values it can't handle. If you perform operations on approximated values, the overall error increases. Never assume a calculated double is exactly what you expect it to be (remember it is a 64 bit value).

    See for example the following link for details:
    http://www.artima.com/underthehood/floating.html
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Wrong converting QVariant to double

    See also qFuzzyCompare().
    J-P Nurmi

Similar Threads

  1. Convert between a custom data type wrapped in a QVariant
    By darkadept in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2009, 09:07
  2. Getting Microsoft Word Object to SaveAs
    By jvwebb in forum Newbie
    Replies: 3
    Last Post: 2nd September 2008, 19:27

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.