Results 1 to 6 of 6

Thread: Numeric rounding problems (Again)

  1. #1
    Join Date
    Jul 2007
    Location
    Jundiai/SP, Brazil
    Posts
    114
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 7 Times in 6 Posts

    Question Numeric rounding problems (Again)

    Hi people,

    The result of 37635.84/3 is 12545.28, then why the system insists on round off the value for 12545.30?
    When I add the result of the division above it is always bigger than the original value .
    I need that the value be not arounding. How I can do that?

    Thanks in advance.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    Default Re: Numeric rounding problems (Again)

    Show us some code. I think that this is a problem with visualisation. Ie. qDebug() prints double values with one digit after decimal point. So value 12545.2799999999999 is printed as 12545.3. And remeber that float/double type have limited precision. Not all real numbers have double representation. Ie. 37635.84 in double variable looks like 37635.839999999997.

  3. #3
    Join Date
    Jan 2010
    Posts
    73
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 8 Times in 8 Posts

    Default Re: Numeric rounding problems (Again)

    Let me guess, you are using a float, not a double. A float, uses 32 bits to store the number.

    First, consider your starting number, 37635.8. This number is not represented exactly the way that the computer stores numbers. The number 3, however, is represented exactly.

    Now, consider actual answer. The answer is 12545.266666666666666666666 (the 6 repeats forever). Think of this as 12545.2 + 2/30. A general rule of thumb is that single precision numbers are good to roughly 7 digits.

    A floating point number in the computer is represented as:

    +/- M * B ^ E

    For the computer, the base B is 2.As a single precision or double precision number, the exponent E is 15 and the sign bit is cleared. So, this lives us with:

    M * 2 ^ 15 = M * 32768

    These are the values for single and double precision (in binary) for M.

    1 .00100110000001111010111
    1 .0010011000000111101011100001010001111010111000010 100

    Now, consider the answer (assuming that it is complete and accurate). With this example, the exponent 16, and the mantissa for the answer is:

    1 .10001000000010100010001
    1 .1000100000001010001000100010001000100010001000100 010

    I think that you might be seeing a pattern... :-)

  4. #4
    Join Date
    Jul 2007
    Location
    Jundiai/SP, Brazil
    Posts
    114
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 7 Times in 6 Posts

    Default Re: Numeric rounding problems (Again)

    Thanks for all replies,

    Qt Code:
    1. bool MyObject::sum()
    2. {
    3. qdouble s=0.0;
    4. for(int j=0;j<=tblwValues->rowCount()-1;++j) {
    5. dtValor = tblwValues->item(j,1);
    6. if(!dtValor)
    7. continue;
    8. s += dtValor->data(0).toDouble();
    9. }
    10.  
    11. if(s < leValor->text().toDouble()) {
    12. qDebug() << "less" << QString::number(s,'f',6) << QString::number(leValor->text().toDouble(),'f',6);
    13. return(FALSE);
    14. }
    15.  
    16. if(s > leValor->text().toDouble()) {
    17. qDebug() << "bigger" << QString::number(s,'f',6) << QString::number(leValor->text().toDouble(),'f',6);
    18. return(FALSE);
    19. }
    20.  
    21. return(TRUE);
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    example:

    item 0: 12545.28
    item 1: 12545.28
    item 2: 12545.28

    Sum: 37635.84 // leValor->text().toDouble()

    Always returns FALSE: qDebug() << "bigger" << QString::number(s,'f',6) << QString::number(leValor->text().toDouble(),'f',6);

    I really don't I know I'm doing something of wrong

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    Default Re: Numeric rounding problems (Again)

    As I told You in previous post. This is a problem with real numbers. Just compile this simple program :
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QVariant>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. double items[3];
    8.  
    9. for( int i = 0; i < 3; i++ )
    10. {
    11. QVariant item("12545.28");
    12. items[i] = item.toDouble();
    13. qDebug() << "item[" << i << "]=" << QString::number(items[i],'f',15);
    14. }
    15.  
    16. double s=0.0;
    17. for( int i = 0; i < 3; i++ )
    18. s += items[i];
    19.  
    20. qDebug() << QString::number(s,'f',15);
    21. }
    To copy to clipboard, switch view to plain text mode 
    Surprised ? If Yes, You must read something about bases of programming.
    This is a reason why in databases we have two types of data : double and numeric.
    Generally real numbers are not good for money calculating.

  6. The following user says thank you to Lesiok for this useful post:

    vcp (5th March 2010)

  7. #6
    Join Date
    Jul 2007
    Location
    Jundiai/SP, Brazil
    Posts
    114
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 7 Times in 6 Posts

    Default Re: Numeric rounding problems (Again)

    All right, all right, I undertood!

    I'm going to use the 'float' and I already did the test and and everything is correctly now

Similar Threads

  1. Problems with rounding
    By estanisgeyer in forum Qt Programming
    Replies: 4
    Last Post: 16th December 2009, 00:50
  2. QDoubleSpinBox rounding
    By Talkless in forum Qt Programming
    Replies: 1
    Last Post: 7th January 2009, 13:40
  3. toDouble() and rounding problem
    By sadjoker in forum Newbie
    Replies: 10
    Last Post: 28th August 2008, 13:47
  4. help with.. numeric mask....
    By ocascante in forum Qt Tools
    Replies: 1
    Last Post: 12th July 2007, 10:53
  5. Banker's rounding
    By matheww in forum General Programming
    Replies: 9
    Last Post: 25th June 2007, 23:22

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.