PDA

View Full Version : use QString::number ( double n, char format = 'g', int precision = 6 )



lzpmail
18th May 2011, 02:52
hi, i find a very strange thing, i use QString::number ( double n, char format = 'g', int precision = 6 ) to show a double type data, the program is run in PC normal, but it run in ARM board, it show -0, and then the program is die, the cpu is up to 85%. but when i use QString::number ( int n, int base = 10 ) , is mean show int type data, the program run in PC or ARM board is well. this is why, and have any method to show double type. thanks.

DanH
18th May 2011, 04:52
Most likely you have the wrong version of code somewhere and there's a mismatch in terms of the number of physical words of parms passed for the double value.

ChrisW67
18th May 2011, 05:08
What does your code actually look like? Where has the double value come from? Does this work as expected:


#include <QtCore>
#include <QDebug>

int main (int argc, char *argv[])
{
QCoreApplication app(argc, argv);
double value = 3.14159265;
qDebug() << QString::number(value, 'g', 6);
return 0;
}

lzpmail
18th May 2011, 06:04
yea, my code is like with you, but the value is not expected, it will change everytime,
this is my code


int fValue; //fValue will changed in other place
QString mStrContext;
mStrContext = QString::number(fValue/10.0, 'g', 3);
pp.drawText(x, y, mStrContext);


when i use qDebug() print the (fValue/10.0) is also be die.

ChrisW67
18th May 2011, 06:33
Is your program crashing or are you just getting unexpected results? If the program is crashing then it is very unlikely that QString::number is involved.

If the value you get out each time is different then it is likely that fValue is not being initialised before you use it (it isn't in your example).

Compile and run my test program on your embedded system. Does it print:
"3.14159"

lzpmail
18th May 2011, 07:56
the program is die, the cpu is up to 85%. when i comment
mStrContext = QString::number(fValue/10.0, 'g', 3); this code, the program run normal. and the fValue is initialized ine the construct function. so it impossible not being initialized before use.

let me run you program, then give you result. thanks

Added after 46 minutes:

in my board it print 4.14378e+95, it very interesting.

ChrisW67
18th May 2011, 08:30
Can you single step through the test program with a debugger? What does the debugger think the value is?

Does the ARM architecture support doubles (Does it work with float)? Are you generating code that matches your architecture and floating point ABI?

MarekR22
18th May 2011, 08:43
1. debug on device (I assume that you have debugged on x86)
2. Qt has small mess with usage of qreal/double, note that qreal on ARM architecture is a float (on other platforms it is a double). In your case (QString::number) double is always used so it should work.
3. Write qtest and if you can reproduce it with a qtest report a bug to qt (Nokia) (include test to report).
I'm pretty sure that this functionality was tested on all platforms so it looks strange that you have a problem with it (see http://qt.gitorious.org/qt/qt/blobs/4.7/tests/auto/qstring/tst_qstring.cpp#line3558 http://qt.gitorious.org/qt/qt/blobs/4.7/tests/auto/qstring/tst_qstring.cpp#line2263).

lzpmail
18th May 2011, 10:02
yea, thanks remind me, i think the ARM do not support double, before i test by small example, it reproduce same proglem, but the example run in PC, it nothing happened. thanks your help.

lzpmail
18th May 2011, 13:07
sorry, i guess is wrong, the ARM9 support soft double type, and i write a sample, the code is


#include <stdio.h>

int main()
{
double a = 3.1415926;
double b = 4.57283;
double result;
result = a/b;

printf("a=%f, b=%f, result=%f\n", a, b, result);

return 0;
}

use arm-linux-gcc-4.3.3 compile, and then let run in ARM board, the printed result is true.

DanH
18th May 2011, 17:58
Depending on the compiler used, "fValue/10.0" will compile to different sized values. Probably technically a bug, but the compiler writer will tell you it's a "feechure". Assign the result to a temp and use the temp:

double tempDbl = fValue/10.0;
mStrContext = QString::number(tempDbl, 'g', 3);


yea, my code is like with you, but the value is not expected, it will change everytime,
this is my code


int fValue; //fValue will changed in other place
QString mStrContext;
mStrContext = QString::number(fValue/10.0, 'g', 3);
pp.drawText(x, y, mStrContext);


when i use qDebug() print the (fValue/10.0) is also be die.

(Actually, it probably isn't technically a compiler bug. "fValue/10.0" can legally compile to a float, not a double.)

MarekR22
18th May 2011, 20:57
Nope you are wrong. C standard says that literal like "10.0" is treated as a double number so you have int/double and this is a double.
Another thing is prototype of QString::number, the only floating point version is version with double (not with qreal) so should work same on ARM as on other platforms.
Like I suggested until program wan't be debugged on arm or some unit test written which proves error in Qt I would assume bug in application not in Qt.

Or maybe you (lzpmail) should give us wider context of usage.

lzpmail
19th May 2011, 08:42
thanks your help, this probram is solved, but i use another method to solve this probram(use sprintf function to change double to char *), but i will continue find the reason, if i find i will tell yours.

lzpmail
20th May 2011, 13:15
hi, before i paste QString::number ( double n, char format = 'g', int precision = 6 ), this problem may have solved, before i use qt 4.5 to compile my program, have problem in ARM board, now i use qt 4.7 to compile, no that problem.

wysota
21st May 2011, 00:18
Is there a question here somewhere?

ChrisW67
21st May 2011, 12:18
No, I don't think so. lzpmail is just sharing the happy news that the problem from here has been solved.

wysota
21st May 2011, 12:22
Threads merged then.