Results 1 to 8 of 8

Thread: double precision or what?

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default double precision or what?

    I coded this:
    Qt Code:
    1. setInput(double* d) {
    2. cout << " *d0 " << d[0] << " " << "*d1 " << d[1] << endl;
    3. for (int i=0; i < _num_input; ++i)
    4. _input[i] = d[i];
    5. }
    6.  
    7. main () {
    8. double input[2] = {4.50, 3.10};
    9. double sum=0;
    10. sum = input[0] + input[1];
    11. for (uint i=0; i < 2; i++)
    12. setInput (input);
    13. cout << "input[0,1] " << input[0] << " " << input[1] << " " << sum << endl;
    14. }
    To copy to clipboard, switch view to plain text mode 
    Output is:
    Qt Code:
    1. *d0 4.5 *d1 3.1000000000000001 //why this '1' at the end???
    2. input[0,1] 4.5 3.1000000000000001 7.5999999999999996
    To copy to clipboard, switch view to plain text mode 
    what's that '1' ???? why does it appear only after setInput() call??
    thanks
    Regards

  2. #2
    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: double precision or what?

    Some decimal values have no accurate representation, because of limited number of bits on which a double value is encoded. That's why floating point variables are not recommended for applications where accuracy is important (like counting money).

  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: double precision or what?

    As far as I remember usually floating point numbers are represented using powers of two, so any floating point number that can't be composed of powers of two (watch the finite length) will not be able to be represented. Maybe 3.10 is one of them

    I think you may read a bit about it here: http://en.wikipedia.org/wiki/Floating_point (I haven't read it, so I can't guarantee, but search for "mantissa" or "significand")

    Edit: You may jump directly here as well: http://en.wikipedia.org/wiki/Double_precision

  4. #4
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: double precision or what?

    ok, but then what do I have to do if I need that high precision in C++? nothing?
    Regards

  5. #5
    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: double precision or what?

    Just round up the value... Do you really need more than 10 digits after the decimal point? You needn't worry about the result as long as it doesn't change the significant digits.

  6. #6
    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: double precision or what?

    Quote Originally Posted by mickey View Post
    what do I have to do if I need that high precision in C++? nothing?
    Write a high-precision number class or use some library.

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: double precision or what?

    the problem isn't only that 3.00000000001 but others too. And with operations error is propagating (increase)...Any hints about library?
    thankks
    Regards

  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: double precision or what?

    Quote Originally Posted by mickey View Post
    the problem isn't only that 3.00000000001 but others too. And with operations error is propagating (increase)...
    As long as it keeps under the number of significant digits you need, you have nothing to worry about.

    As for the lib, try this one: http://www.nongnu.org/hpalib

    Or this one: http://freshmeat.net/projects/ccmath/

    In any case, google is your friend.

Similar Threads

  1. QGraphicsScene Click / Double Click
    By philentropist in forum Qt Programming
    Replies: 1
    Last Post: 9th February 2007, 04:32
  2. precision problem - Help pls
    By munna in forum General Programming
    Replies: 7
    Last Post: 26th May 2006, 04:00
  3. QSlider with double Heads..
    By moizahamedv in forum Qt Tools
    Replies: 5
    Last Post: 9th May 2006, 14:28
  4. Push button double click
    By curtisw in forum Qt Programming
    Replies: 3
    Last Post: 15th February 2006, 16:40
  5. Replies: 5
    Last Post: 12th January 2006, 15:40

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.