Results 1 to 8 of 8

Thread: Wrong in qt calculation?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Wrong in qt calculation?

    Hi,
    I am pulling my hair.....
    I am trying to send a Rate to a pump (throung RS-232). I have done this is qt3 about 6 years ago and I am know converting it all to qt4.

    The problem is that I (according to me and my calculations in excel) that qt is calculating the rate almost right, but every other timevalue(second) is calculates it wrong.

    Part of source code:

    Qt Code:
    1. void pump::loop()
    2. {
    3. float amount;
    4. float rate;
    5.  
    6. amount = getAmount(kB, vf, (timeValue), bC);
    7. rate = getRate(kA, kB, vf, (timeValue),bC);
    8.  
    9. timeValue = timeValue + 1000;
    10.  
    11. float myQtApp::getAmount(float kB, float vf, float timeValue, float bC)
    12. {
    13. float amount;
    14. amount=expf(-kB*timeValue)*bC*vf;
    15. float myQtApp::getRate( float kA, float kB, float vf, float timeValue, float bC)
    16. return(amount);
    17. }
    18.  
    19. float myQtApp::getRate( float kA, float kB, float vf, float timeValue, float bC)
    20. {
    21. float rate;
    22. float ga1 = getAmount(kB,vf,timeValue, bC);
    23. float ga2 = getAmount(kB,vf, (timeValue+1), bC);
    24.  
    25. rate = -(expf(-kA) * ga1 - ga2); // it is here the calculation problem ocuurs
    26. return(rate);
    27. }
    To copy to clipboard, switch view to plain text mode 
    it if very small number so I think an expanation could possibly be that qt rounds off the values. How can I tell qt to include as many decimals as possible in this calculations?

    Thanks in advance!
    Thomas
    Last edited by wysota; 8th April 2011 at 11:37. Reason: missing [code] tags

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

    Default Re: Wrong in qt calculation?

    Qt is not calculating anything (you're not even using a single line of code related to Qt here), it's your CPU that performs calculations based on what the C/C++ runtime provides to it. Remember that float type has a limited precision.
    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.


  3. #3
    Join Date
    Apr 2010
    Posts
    769
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 94 Times in 86 Posts

    Default Re: Wrong in qt calculation?

    I agree with wysota. Try using type 'double' instead of type 'float', and use the double form of exp().

  4. #4
    Join Date
    Apr 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Wrong in qt calculation?

    Hi,
    Thank for answer
    I changed float to double but I still have the same problem.

    Can I add some code that tells c++ to use as many decimals as possible.
    /Thomas

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

    Default Re: Wrong in qt calculation?

    You can use a fixed point arithmetics library.
    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.


  6. #6
    Join Date
    Apr 2010
    Posts
    769
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 94 Times in 86 Posts

    Default Re: Wrong in qt calculation?

    C++ always performs computations at the full precision allowed by the data type. If doubles aren't working, you can try to convert the problem into an integer expression, or analyze the order of operations to try and minimize effects caused by limited precision, such as adding a very small number to a relatively very large number. Again, wysota's suggestion of using fixed point point math is also viable, although it may not be necessary.

    If you could post the inputs, intermediate results and outputs of your functions with two calls, and the expected results and discrepancies, we might be able to lend more assistance.

    ------------------------

    Something else to consider: what, exactly, is "wrong" with the results? It's entirely possible that Excel is doing some rounding itself when displaying results; C++ may also not be displaying the full precision of the numbers used, depending on how you're outputting them.
    Last edited by SixDegrees; 8th April 2011 at 13:34.

  7. #7
    Join Date
    Apr 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Wrong in qt calculation?

    You are the best!
    I removed the expf and replaced with exp and it now works perfectly!
    Many many thanks!!
    /Thomas

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

    Default Re: Wrong in qt calculation?

    Just to be sure - you did change the expf() call to exp(), right? And you are sure the order of operations is correct, I mean that the brackets are positioned correctly?
    Qt Code:
    1. rate = -(exp(-kA) * ga1 - ga2);
    To copy to clipboard, switch view to plain text mode 
    vs
    Qt Code:
    1. rate = -(exp(-kA) * (ga1 - ga2));
    To copy to clipboard, switch view to plain text mode 
    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. Exponential calculation
    By bollibompa in forum Newbie
    Replies: 1
    Last Post: 4th April 2011, 23:11
  2. QTime to a format which I can use in a calculation
    By Gavin Harper in forum Qt Programming
    Replies: 4
    Last Post: 14th March 2011, 23:15
  3. Monetary calculation
    By estanisgeyer in forum General Programming
    Replies: 1
    Last Post: 15th February 2008, 12:35
  4. Compiler's calculation on memory/address
    By vermarajeev in forum General Discussion
    Replies: 3
    Last Post: 10th October 2007, 11:25
  5. what to do during long calculation?
    By Weilor in forum Qt Programming
    Replies: 4
    Last Post: 14th September 2007, 12:11

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
  •  
Qt is a trademark of The Qt Company.