need help with operations stability

Discussion in 'C++' started by mihai, Mar 2, 2005.

  1. mihai

    mihai Guest

    Yelloo ...
    I must write a C++ program for my computational geometry class,
    and I need some references where can I solve the next problem:

    POINT P(1, 0);

    for (i=0;i<96;++i)
    P.rotate(PI/2);

    Now P must be at (1, 0) ...
    but it isn't ...

    How can I solve this the problem;
    the rotate function in implemented with
    standard math sin and cos functions.

    Thanks
    Mihai.
     
    mihai, Mar 2, 2005
    #1
    1. Advertising

  2. mihai

    Tim Love Guest

    "mihai" <> writes:

    >Yelloo ...
    >I must write a C++ program for my computational geometry class,
    >and I need some references where can I solve the next problem:
    > ...
    >Now P must be at (1, 0) ...
    >but it isn't ...

    Is it very close or a long way away. Is it the same each time you
    try it?

    >How can I solve this the problem;
    >the rotate function in implemented with
    >standard math sin and cos functions.

    Maybe you could post your rotate code?
     
    Tim Love, Mar 2, 2005
    #2
    1. Advertising

  3. mihai

    mihai Guest

    The code is like this:

    P::rotate(double rad)
    {
    double xx = _x;
    double yy = _y;
    _x = xx * cos(rad) + yy * sin(rad);
    _y = xx * cos(rad) - yy * sin(rad);
    }

    And I must say that the result is close ... but I can not do P==Q after
    that.
     
    mihai, Mar 2, 2005
    #3
  4. mihai

    Tim Love Guest

    "mihai" <> writes:

    >The code is like this:


    >P::rotate(double rad)
    >{
    > double xx = _x;
    > double yy = _y;
    > _x = xx * cos(rad) + yy * sin(rad);
    > _y = xx * cos(rad) - yy * sin(rad);
    >}


    >And I must say that the result is close ... but I can not do P==Q after
    >that.

    Perhaps you shouldn't be trying to do that. Look at
    http://www.eason.com/library/math/floatingmath.pdf
    What Every Computer Scientist Should Know About Floating-Point Arithmetic
     
    Tim Love, Mar 2, 2005
    #4
  5. mihai wrote:
    >
    > Yelloo ...
    > I must write a C++ program for my computational geometry class,
    > and I need some references where can I solve the next problem:
    >
    > POINT P(1, 0);
    >
    > for (i=0;i<96;++i)
    > P.rotate(PI/2);
    >
    > Now P must be at (1, 0) ...
    > but it isn't ...


    No wonder. Your code accumulates all the errors.
    Don't do that if you can avoid it.

    POINT P(1,0);

    for( i = 0; i < 96; ++i ) {
    POINT Q(P);
    Q.rotate( i * PI/2 );
    }

    In other words:
    Instead of small incremental rotations on the point
    itself, start with a fresh point and rotate that point
    only once (but this time, with an angle identical to
    the idealized summed up angles).

    Other things to consider:
    How do you determine that the point is not at (1,0)?
    Could it be that you are using plain vanilla '==' ?

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Mar 2, 2005
    #5
  6. > I must write a C++ program for my computational geometry class,
    > and I need some references where can I solve the next problem:
    >
    > POINT P(1, 0);
    >
    > for (i=0;i<96;++i)
    > P.rotate(PI/2);
    >
    > Now P must be at (1, 0) ...
    > but it isn't ...


    Maybe your PI/2 constant is wrong. Check if sin(PI/2)==1 and cos(PI/2)==0.
    If they are, then you have an error in the rotate function. If not then
    either PI/2 is wrong or your sin and cos functions are wrong. I have heard
    that sin and cos implemented in the intel processors FPU are a little bit
    wrong (thats why Java does not use the trig. functions in the FPU, but uses
    its own), so that might be the cause.

    However in general it is better not to rely on float/double calculations
    being precise.

    Niels Dybdahl
     
    Niels Dybdahl, Mar 2, 2005
    #6
  7. Niels Dybdahl wrote:
    >
    > > I must write a C++ program for my computational geometry class,
    > > and I need some references where can I solve the next problem:
    > >
    > > POINT P(1, 0);
    > >
    > > for (i=0;i<96;++i)
    > > P.rotate(PI/2);
    > >
    > > Now P must be at (1, 0) ...
    > > but it isn't ...

    >
    > Maybe your PI/2 constant is wrong. Check if sin(PI/2)==1 and cos(PI/2)==0.
    > If they are, then you have an error in the rotate function. If not then
    > either PI/2 is wrong or your sin and cos functions are wrong.


    Define 'wrong'.

    In particular I would be interested how you would define PI/2 in
    a computer language with only a limited number of bits, such that the
    value is not 'wrong'.

    >
    > However in general it is better not to rely on float/double calculations
    > being precise.


    That however is the cause of the OP's problem. Limited precision
    due to limited bits in calculation.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Mar 2, 2005
    #7
  8. mihai

    msalters Guest

    Karl Heinz Buchegger wrote:
    > mihai wrote:


    > > POINT P(1, 0);
    > >
    > > for (i=0;i<96;++i)
    > > P.rotate(PI/2);


    > No wonder. Your code accumulates all the errors.
    > Don't do that if you can avoid it.
    >
    > POINT P(1,0);
    >
    > for( i = 0; i < 96; ++i ) {
    > POINT Q(P);
    > Q.rotate( i * PI/2 );
    > }


    That's still not as good as it (c|sh)ould be. You should avoid
    angles much bigger than 2PI. In this case:

    POINT P(1,0);
    for( i = 0; i < 96; ++i ) {
    POINT Q(P);
    Q.rotate( (i%4) * PI/2 );
    }
    since sin(4 * (PI/2)) == sin(0) == 0.

    HTH,
    Michiel Salters
     
    msalters, Mar 2, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dominic
    Replies:
    1
    Views:
    3,279
    Bernard
    Dec 14, 2004
  2. TCORDON

    Website stability

    TCORDON, Mar 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,172
    JohnFol
    Mar 21, 2005
  3. nos

    unicode stability

    nos, Jan 20, 2004, in forum: Java
    Replies:
    2
    Views:
    296
    Thomas Weidenfeller
    Jan 20, 2004
  4. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,247
    robert
    Feb 11, 2006
  5. Karl Groves
    Replies:
    4
    Views:
    549
    SpaceGirl
    Jul 29, 2004
Loading...

Share This Page