Results 1 to 3 of 3

Thread: How to get exact value of double

  1. #1
    Join Date
    May 2017
    Posts
    14
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default How to get exact value of double

    How can one dump the exact value of a double variable to the console/terminal? I can do something like this:
    Qt Code:
    1. // var1 & var2 are both set elsewhere
    2. qDebug() << var1;
    3. qDebug() << var2;
    4. qDebug() << QString::number(var1,'f',4);
    5. qDebug() << QString::number(var2,'f',4);
    6. qDebug() << (var1 == var2);
    To copy to clipboard, switch view to plain text mode 

    This result will be:
    Qt Code:
    1. 0.05
    2. 0.05
    3. 0.0500
    4. 0.0500
    5. false
    To copy to clipboard, switch view to plain text mode 


    I have worked around these issues in the past with silly stuff like:
    Qt Code:
    1. var1 = (double)((int)((var1*100)+0.5))/100;
    To copy to clipboard, switch view to plain text mode 

    I'm not asking for the best workaround for rounding these numbers, merely what is the easiest way to dump the exact value of the variable, and not have it formatted via QT in some way?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,243
    Thanks
    303
    Thanked 866 Times in 853 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to get exact value of double

    This thread from stackoverflow might help.

    Basically, you would use QString::number() as you have been, but replace the "4" with the value obtained from:

    Qt Code:
    1. std::numeric_limits< double >::max_digits10
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    May 2017
    Posts
    14
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to get exact value of double

    Thank you! Can't try it now, but this is right up to speed with what I was asking for.

Similar Threads

  1. How to find out the exact issue in Qt
    By shivcena in forum Qt Programming
    Replies: 0
    Last Post: 4th November 2015, 09:25
  2. Replies: 1
    Last Post: 12th March 2014, 09:28
  3. Execute a funcion at an exact time of day
    By KillGabio in forum Qt Programming
    Replies: 5
    Last Post: 28th May 2013, 09:08
  4. get exact height of a QString in pixels
    By zack in forum Qt Programming
    Replies: 1
    Last Post: 14th July 2010, 14:16
  5. exact region of drawn part
    By sanjayshelke in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2008, 19:07

Tags for this Thread

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.