Home > Enterprise >  Strange manner in multiplying a double value by 100000 in Qt (c )
Strange manner in multiplying a double value by 100000 in Qt (c )

Time:01-13

Can anybody explain this:

double nnn = 2678.87;
QVariant vvv(2678.87);
qDebug() << qRound(nnn*1000.0);    //--> 2678870
qDebug() << qRound(nnn*10000.0);   //--> 26788700
qDebug() << qRound(nnn*100000.0);  //--> 267887000
qDebug() << qRound(nnn*1000000.0); //--> 2147483647 !
qDebug() << qRound(vvv.toDouble()*1000000.0);  //--> -2147483648 !!!

The two last statements also have strange result in following format:

qDebug() << qRound(nnn*1000000); //--> 2147483647 !
qDebug() << qRound(vvv.toDouble()*1000000);  //--> -2147483648 !!!

I need to use last statement but it is wrong apparently!

CodePudding user response:

qRound returns an int, your value is larger than will fit in an int without overflowing. try qround64 instead.

Note you'll still eventually run into precision problems, a double can only exactly represent integers up to around 9 * 1015.

  • Related