Results 1 to 8 of 8

Thread: precision problem - Help pls

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default precision problem - Help pls

    Hi,

    I am doing some calculations with double precision 8. Here is the problem:

    First I calculate 1/6 which gives me 0.16666667 and then to this result I multiply 3 which gives me 0.50000001, but the ideal solution should be 0.5.

    If I try the same thing with a calculator I get the ideal solution. How can I also get something like calculator - shoe maximum precision where ever needed and show least whereever not needed.

    Thanks a lot.

  2. #2
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: precision problem - Help pls

    I don't know how you can do what you want... But I can give you some counsel concerning what I know... First of all, it is better to avoid useless calculation (like division and then multiplication) , you should do that by hand (if possibile) and use the final expression. Another thing would be particular numerical methos that can spare you some calculation (and the consequent error). For and "easy" introduction (but the methos showed can be out of date and not so theoretically "clean") to numerical methos you can take a look at Numerical Recipes http://www.nr.com/.
    I hope it could help.

  3. #3
    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: precision problem - Help pls

    Not all values can be stored in floating point types and they get rounded to the nearest value (in this case 0.16666667).

    1.0/6 = 0.16666667 (there is no way of storing 2/3 in a float or double)
    0.16666667*3 = 0.(...)1 (because 7*3=21, so the result has to end with "1")

    If you agree to loose one decimal place of the result, you can round the value and receive the proper result (0.5000000).

    Another workaround is not to operate on floating point values as long as you can:

    (1/6)*3 = 3*(1/6) = 3/6 = 0.5

  4. #4
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: precision problem - Help pls

    The example I gave was part of a big problem which the user gives. Therefore, I dont have much control over what user can enter. If give the same problem in windows calculator, it gives the answer perfectly i.e., 1/6 = 0.16666667 and then multiplying it with 3 gives 0.5.

    Any idea on this ?

    Thanks a lot.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: precision problem - Help pls

    Suppose that your number can have more than 8 digits after the decimal place.

    This way you have:
    > x = 1 / 6
    x = 0.16666666...67
    > x = 3 * x
    x = 0.50000000...01

    What would happen if you were displaying those numbers only up to 8th decimal place (of course rounded and without meaningless zeroes)?

  6. #6
    Join Date
    May 2006
    Location
    Bangalore
    Posts
    23
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb Re: precision problem - Help pls

    Quote Originally Posted by wysota
    Another workaround is not to operate on floating point values as long as you can:

    (1/6)*3 = 3*(1/6) = 3/6 = 0.5
    Sticking with the above advise. I would like to maintain 2 floating variables.
    Qt Code:
    1. float numerator = 1, denominator = 1;
    To copy to clipboard, switch view to plain text mode 

    Whenever you have to multiply fractions, keep multiplying numerators to the numerator, as well as keep multiplying denominators to the denominator.

    Qt Code:
    1. //multiply (1/6)
    2. numerator *= 1;
    3. denominator *=6;
    4.  
    5. //multiply (3)
    6. numerator *= 3;
    7. denominator *=1; // since 3 can also be written as (3/1)
    To copy to clipboard, switch view to plain text mode 
    In the end calculate
    Qt Code:
    1. result = numerator/denominator
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    May 2006
    Posts
    32
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Wink Re: precision problem - Help pls

    I try this code snippet in VC6

    Then I got 0.5--The same as Windows small caculator.

    Qt Code:
    1. int main(int argc, char* argv[])
    2. {
    3. double i;
    4. /*
    5.   usr input stack of caculator.
    6.   */
    7. i = 1* 1.0 / 6;
    8. i * =3 * 1.0 ;
    9.  
    10. return 0;
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by iGoo; 26th May 2006 at 02:54.

  8. #8
    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: precision problem - Help pls

    The compiler probably optimised that during compilation.

Similar Threads

  1. Problem in using QHttp with QTimer
    By Ferdous in forum Newbie
    Replies: 2
    Last Post: 6th September 2008, 12:48
  2. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  3. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  4. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.