PDA

View Full Version : How to get exact value of double



while_e
27th September 2017, 17:45
How can one dump the exact value of a double variable to the console/terminal? I can do something like this:


// var1 & var2 are both set elsewhere
qDebug() << var1;
qDebug() << var2;
qDebug() << QString::number(var1,'f',4);
qDebug() << QString::number(var2,'f',4);
qDebug() << (var1 == var2);


This result will be:


0.05
0.05
0.0500
0.0500
false



I have worked around these issues in the past with silly stuff like:


var1 = (double)((int)((var1*100)+0.5))/100;


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?

d_stranz
27th September 2017, 20:34
This thread from stackoverflow might help. (https://stackoverflow.com/questions/554063/how-do-i-print-a-double-value-with-full-precision-using-cout)

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


std::numeric_limits< double >::max_digits10

while_e
27th September 2017, 20:50
Thank you! Can't try it now, but this is right up to speed with what I was asking for.