I have a problem using the GMP library in a Qt project under Windows XP SP2.
When I run my Qt application, sometimes it works and sometimes it doesn't. When the application crashes, it display a warning with the message "Microsoft Visual C++ runtime Library run time error" or hangs for a few minutes. Sometimes it works (and there is no error)
Here are some specifics:
i used QT ver 4.3.2 (open source) compiler: mingw32 ver : GNU Make 3.80 GCC : 3.4.5 (mingw special) G++ : 3.4.5 (mingw special) gmp 4.3.2 my cpu type :x86 family
I had compiled the GMP library successfully like this using MSYS 1.0.11:
./configure --prefix=/gmp/install --enable-cxx
make
make install
Here are my Qt pro.file contents :
..
..
INCLUDEPATH += C:/LIBGMP
LIBS += -LC:/LIBGMP -lgmpxx -lgmp
Here is a part of my application function in Qt using GMP library :
QString gmptambah(QString angka1 ,QString angka2,int angkapresisi) const
{
mpf_set_default_prec(angkapresisi);
mpf_t x,y,z;
mpf_init (x);
mpf_init (y);
mpf_init (z);
QByteArray encodedString1=angka1.toAscii ();
const char *x1= encodedString1.constData ();
QByteArray encodedString2=angka2.toAscii ();
const char *y1= encodedString2.constData ();
mpf_set_str (x,x1,10);
mpf_set_str (y,y1,10);
mpf_add (z,y,x);
mp_exp_t exponent ;
mpf_class nilaiz(z);
nilaiz.set_prec(angkapresisi);
QString hasil((nilaiz.get_str(exponent, 10, 0)).c_str()); // this is my problem
long int exp=(exponent);
if(exp>=0)
{
if(hasil.at(0)!='-' && exp==0)
{
hasil.insert(0,"0"); exp++;
}
else if(hasil.at(0)=='-' && exp==0)
{
hasil.insert(1,"0");
exp=exp+2;
}
else if(hasil.at(0)=='-' && exp>=1)
{
exp=exp+1;
}
hasil.insert(exp,".");
hasil.replace(QString(" "), QString("0"));
QStringList hasiltmp;
hasiltmp=hasil.split(".");
if(hasiltmp.size()==1) hasil.append("0");
else if(hasiltmp.size()==2)
{
if(hasiltmp.at(1)=="")hasil.append("0");
}
}
else
{
if(hasil.at(0)!='-') for(int i=0;i<qAbs(exp);i++)hasil.insert(0,"0");
else for(int i=0;i<qAbs(exp);i++)开发者_如何学Gohasil.insert(1,"0");
if(hasil.at(0)!='-') hasil.insert(0,"0.");
else hasil.insert(1,"0.");
}
mpf_clear (x);
mpf_clear (y);
mpf_clear (z);
x1=0; y1=0;
return pembulatan(hasil);
}
After tracing the problem, I found that my problem is located at nilaiz.get_str(exponent, 10, 0)
function which called the mpf_get_str
function in gmpxx.h
When I remove it from my application (without calling the nilaiz.get_str(exponent, 10, 0)
function), the application runs well without errors.
But I need that function to convert mpf_t z
to QString
(string in Qt or char *
).
Why does this happen? What should I do next? Or am I missing something when I compile the GMP library or should I add an xtra option when I configure GMP or there is a bug using that function?
I need help or suggestions. Thank you!
Best regards
aansd
精彩评论