Results 1 to 17 of 17

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

  1. #1
    Join Date
    Mar 2011
    Posts
    42
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

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

    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.

  2. #2
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    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.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    What does your code actually look like? Where has the double value come from? Does this work as expected:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. int main (int argc, char *argv[])
    5. {
    6. QCoreApplication app(argc, argv);
    7. double value = 3.14159265;
    8. qDebug() << QString::number(value, 'g', 6);
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  4. #4
    Join Date
    Mar 2011
    Posts
    42
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    yea, my code is like with you, but the value is not expected, it will change everytime,
    this is my code
    Qt Code:
    1. int fValue; //fValue will changed in other place
    2. QString mStrContext;
    3. mStrContext = QString::number(fValue/10.0, 'g', 3);
    4. pp.drawText(x, y, mStrContext);
    To copy to clipboard, switch view to plain text mode 

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

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    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:
    Qt Code:
    1. "3.14159"
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Mar 2011
    Posts
    42
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    the program is die, the cpu is up to 85%. when i comment
    Qt Code:
    1. mStrContext = QString::number(fValue/10.0, 'g', 3);
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by lzpmail; 18th May 2011 at 07:56.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    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?

  8. #8
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    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/...g.cpp#line3558 http://qt.gitorious.org/qt/qt/blobs/...g.cpp#line2263).
    Last edited by MarekR22; 18th May 2011 at 08:47.

  9. #9
    Join Date
    Mar 2011
    Posts
    42
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    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.

  10. #10
    Join Date
    Mar 2011
    Posts
    42
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    sorry, i guess is wrong, the ARM9 support soft double type, and i write a sample, the code is
    Qt Code:
    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5. double a = 3.1415926;
    6. double b = 4.57283;
    7. double result;
    8. result = a/b;
    9.  
    10. printf("a=%f, b=%f, result=%f\n", a, b, result);
    11.  
    12. return 0;
    13. }
    To copy to clipboard, switch view to plain text mode 
    use arm-linux-gcc-4.3.3 compile, and then let run in ARM board, the printed result is true.

  11. #11
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    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:
    Qt Code:
    1. double tempDbl = fValue/10.0;
    2. mStrContext = QString::number(tempDbl, 'g', 3);
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by lzpmail View Post
    yea, my code is like with you, but the value is not expected, it will change everytime,
    this is my code
    Qt Code:
    1. int fValue; //fValue will changed in other place
    2. QString mStrContext;
    3. mStrContext = QString::number(fValue/10.0, 'g', 3);
    4. pp.drawText(x, y, mStrContext);
    To copy to clipboard, switch view to plain text mode 

    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.)

  12. #12
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    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.

  13. #13
    Join Date
    Mar 2011
    Posts
    42
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: use QString::number ( double n, char format = 'g', int precision = 6 )

    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.

  14. #14
    Join Date
    Mar 2011
    Posts
    42
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Talking QString::number ( double n, char format = 'g', int precision = 6 )title talk again

    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.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString::number ( double n, char format = 'g', int precision = 6 )title talk agai

    Is there a question here somewhere?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #16
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QString::number ( double n, char format = 'g', int precision = 6 )title talk agai

    No, I don't think so. lzpmail is just sharing the happy news that the problem from here has been solved.

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString::number ( double n, char format = 'g', int precision = 6 )title talk agai

    Threads merged then.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Print floating and double precision number
    By marc2050 in forum Newbie
    Replies: 2
    Last Post: 17th May 2011, 08:15
  2. Replies: 7
    Last Post: 28th December 2010, 20:27
  3. The precision range of double
    By nikhilqt in forum Qt Programming
    Replies: 14
    Last Post: 8th July 2009, 15:08
  4. strange problem about QString::number(double)
    By yuzr in forum Qt for Embedded and Mobile
    Replies: 4
    Last Post: 24th December 2007, 13:05
  5. double precision or what?
    By mickey in forum General Programming
    Replies: 7
    Last Post: 20th February 2007, 20:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.