PDA

View Full Version : Cannot convert double to QString



maxpower
7th February 2007, 16:43
I am running Qt 4.2.2 on FreeBSD 4.8 with gcc 3.4.6. I am working with dollar amounts in doubles but I cannot properly convert these doubles into QStrings for display in a QLabel. As a test I compiled the following very simple code:



#include <QtCore>
int main () {
double doublevar = 42.84;

qDebug() << QString::number(doublevar);
}


Which outputs "-0" to the terminal. What could be causing this? The same code in this simple program runs fine on my Linux and Windows boxes. If I use a QVariant as below:

#include <QtCore>
int main () {
double doublevar = 42.84;

QVariant var(doublevar);
qDebug() << var.toString();
}

The output is "-0.00000000005965e+224". Again, what could be wrong?

Thanks
mAx

wysota
7th February 2007, 20:09
This indeed is strange. The output you experience could be caused by mixups caused by incompatibility between your machine (CPU and OS) and the Qt library, but I think that in this cause nothing should run at all. What is the result of the following?


double d = 42.84;
qDebug() << d;

maxpower
7th February 2007, 20:17
This


#include <QtCore>
int main () {
double d = 42.84;
qDebug() << d;
}

still gives "-0"

Thanks
mAx

gfunk
7th February 2007, 20:20
What compiler options do you use, maybe there are some special switches for double floating point (64-bit)?

maxpower
7th February 2007, 20:47
I have not edited the default settings that Qt defaulted to. Here is what is given during a make:



g++34 -c -pipe -O2 -Wall -W -pthread -D_THREAD_SAFE -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I../../../local/Trolltech/Qt-4.2.2/mkspecs/freebsd-g++34 -I. -I../../../local/Trolltech/Qt-4.2.2/include/QtCore -I../../../local/Trolltech/Qt-4.2.2/include/QtCore -I../../../local/Trolltech/Qt-4.2.2/include/QtGui -I../../../local/Trolltech/Qt-4.2.2/include/QtGui -I../../../local/Trolltech/Qt-4.2.2/include -I. -I. -I. -I../../../local/include -o foo.o foo.cpp


Thanks
mAx

wysota
7th February 2007, 21:05
And this one?


printf("%lf\n", 42.84);

maxpower
7th February 2007, 21:17
That gives: 42.840000

wysota
7th February 2007, 22:33
Did you compile Qt yourself or do you use some precompiled package? What exact architecture do you have? x86?

maxpower
7th February 2007, 22:47
Yes it is x86 and I did compile Qt myself as I could only find Qt 3 for FreeBSD 4.8 in package format.

Thanks
mAx

yuzr
24th December 2007, 04:04
Some ARM installations have floating point implementations that are not fully IEEE-compliant. In Qt/Embedded 3.3.x, this causes QString::number(double) and related functions to give incorrect results. A fix for this was introduced in Qt 3.3.2: defining the macro QT_QLOCALE_USES_FCVT will make Qt use system library code instead, i.e:

./configure -embedded arm -DQT_QLOCALE_USES_FCVT ...

i am in the face of this problem,i have added the macro,but it doesn't work.