Friday, May 2, 2014

Decimal for QT C++

We all know not to use primitive data type double for any arithmetic calculation involves financial or sensitive data which  rounding errors is not acceptable. In that case, Java has java.math.BigDecimal, C# can use System.Decimal, but there is no Decimal built in support for C++.
However there are a few third party libraries to the rescue.
  1. GNU Multiple Precision Arithmetic Library.
  2. qdecimal
  3. Intel's floating point math library
We are going to focus on qdecimal, which specifically target QT C++ at this post.
Because I use Mingw-32 QT version, the library I download does not compile properly, so instead I include the source code into my project.


#include <QCoreApplication>
#include <QtConcurrent/QtConcurrent>
#include "qdecimal/QDecDouble.hh"
#include "qdecimal/QDecNumber.hh"


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QDecDouble decDouble = 10.00;
    QDecDouble result = decDouble / 90;
    QTextStream(stdout) << result.toString();
    return a.exec();
}


that's it, hop it helps.

No comments:

Post a Comment