Results 1 to 12 of 12

Thread: QPolygonF precision?! is it possible?!

  1. #1
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QPolygonF precision?! is it possible?!

    I have this QPolygonF

    Qt Code:
    1. selectedPolygon<< QPointF(1.47 ,52.21);
    2. selectedPolygon<< QPointF(1.50 ,53.12);
    3. selectedPolygon<< QPointF(1.52 ,54.48);
    4. selectedPolygon<< QPointF(1.55,55.84);
    5. selectedPolygon<< QPointF(1.57 ,57.20);
    6. selectedPolygon<< QPointF(1.60 ,58.57);
    7. .....................................
    8. selectedPolygon<< QPointF(1.73 ,66.28);
    9. selectedPolygon<< QPointF(1.75 ,68.10);
    10. selectedPolygon<< QPointF(1.78 ,69.92);
    11. selectedPolygon<< QPointF(1.80,72.19);
    12. selectedPolygon<< QPointF(1.83 ,74.46);
    To copy to clipboard, switch view to plain text mode 

    and in debug mode when I get back these values:
    Qt Code:
    1. int N = selectedPolygon.size();
    2. for (int i = 0; i < N; i++)
    3. {
    4. x = selectedPolygon.at(i).x();
    5. y = selectedPolygon.at(i).y();
    6. ........
    7. }
    To copy to clipboard, switch view to plain text mode 

    the values comeback as:
    Qt Code:
    1. (1.47, 52.210000000000001)
    2. (1.5, 53.119999999999997)
    3. (1.52, 54.479999999999997)
    4. (1.55, 55.840000000000003)
    5. (1.5700000000000001, 57.200000000000003)
    6. .........................
    To copy to clipboard, switch view to plain text mode 

    this change causes huge errors to my calculations!!!!!!
    why is this happening? is there a way to avoid it??!
    Last edited by fatecasino; 12th January 2011 at 05:02. Reason: reformatted to look better

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QPolygonF precision?! is it possible?!

    This happens because computers (any computer) have finite precision (rounding errors for example)
    When dealing with precision calculations, this is a huge factor to deal with.

    It is however possible to do precision calculations with the help of a few tricks.

    In your case, I think it is a good idea to round the number to the nearest best value, 2 decimals. As you notice, your computer can be a lot more precise than the numbers you use, you just have to round them once more yourself. This (dealing with the precision of a computer) can be a speed penalty.
    Also make sure you use the same kind of variable when reading or setting the values to minimize conversions.

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

    Default Re: QPolygonF precision?! is it possible?!

    Also, if these tiny discrepancies are causing "huge errors" in your calculations, you need to rethink those calculations in terms of the limits imposed by floating point math operations. For example, you should never take branches based on a floating point number being identically equal to another; instead, you should check that they are within some small distance. The C++ STL provides some support for this.

  4. #4
    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: QPolygonF precision?! is it possible?!

    To visualize the problem try to write 1/3 in decimal based number system, you will get 0.333333(3).
    So same problem is with 1/10 in binary based system. When you converting numbers from decimal to binary (with limit of bits) and then back from binary to decimal the result might be different then initial value.

  5. #5
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPolygonF precision?! is it possible?!

    you are definitely right! In my Msc I did a lot of Numerical analysis, but I never met such a thing.
    Finite precision errors would occur in exceptional situations that I should take care.
    like finding the solution of

    (x-0.0000000000000000001)*(x-0.1)*(x-0.0000000000099), or whatsoever.

    Now it's like storing a simple value "1.52" and never been able to receive it back as it is (1.52).
    I have used several programming languages but I don't remember having such a problem before.
    Can you suggest any Qt function to round or even chop off digits?(if this is the only solution)

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QPolygonF precision?! is it possible?!

    Check http://doc.trolltech.com/latest/qtglobal.html

    This isn't something specific to a programming language, this is inherent to any computer.

  7. #7
    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: QPolygonF precision?! is it possible?!

    There are libraries for fixed point arithmetics available. What they do is that they store numbers in a custom format (like storing digits separaterly) and guarantee you the exact precision you declare. The downside is that calculations using such numbers are much slower because they have to be implemented in software.
    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.


  8. #8
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPolygonF precision?! is it possible?!

    Knowing the nature of my datasets i try to set the precision at the 4th decimal digit.
    So, what happens so far is that

    actual value read from data file: 0.1
    value after converting QString to Double (toDouble() ): 0.10000000000000001
    then I created this rounding function
    Qt Code:
    1. double my2dPlot::round_nplaces(double value, int to)
    2. {
    3. double a, b,c, places = pow(10.0, to);//to = 4, places = 10000
    4. a= value * places;// a = 1000
    5. b= round(value * places);// b = 1000
    6. c = b/ places;// c = 0.10000000000000001 ????!
    7. return round(value * places) / places;
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    how is it possible to get c = 1000/10000 = 0.10000000000000001 ?!
    shouldn't it be 0.1?

  9. #9
    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: QPolygonF precision?! is it possible?!

    No, computers work in binary and not decimal values. Apparently there is no binary representation for 1000/10000. If you haven't done so yet, read this: http://en.wikipedia.org/wiki/Floating_point
    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.


  10. #10
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPolygonF precision?! is it possible?!

    very good article, thanks wysota!
    hence
    if I cannot represent a float/double with the precision i want,
    if I cannot round the represented float number to the digit I want,
    what is the proposed solution to read in the string "0.5" from a text and store to a double/float value as 0.5?

  11. #11
    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: QPolygonF precision?! is it possible?!

    See post #7.
    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.


  12. #12
    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: QPolygonF precision?! is it possible?!

    Quote Originally Posted by fatecasino View Post
    if I cannot round the represented float number to the digit I want,
    For the purposes of display in decimal there is nothing to stop you rounding the floating point number at a suitable number of decimal digits: see QString::number(). If you are using a float (32-bit) then you can expect about 7 significant digits and double gives 16. Your example data only has four or five digits. For the vast majority of store, retrieve and light manipulation purposes this quite adequate.

Similar Threads

  1. Error in QPolygonF::subtracted
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 25th June 2010, 02:27
  2. Sorting QPolygonF
    By giusepped in forum Qt Programming
    Replies: 3
    Last Post: 7th January 2009, 09:34
  3. a crazy problem~~~who can help me : (
    By sunking_426 in forum Qt Programming
    Replies: 4
    Last Post: 2nd January 2009, 15:35
  4. Crazy over the QGraphicsScene/Item
    By Morea in forum Qt Programming
    Replies: 6
    Last Post: 20th November 2006, 10:18

Tags for this Thread

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.