# need help with operations stability

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

1. ### mihaiGuest

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

2. ### Tim LoveGuest

"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

3. ### mihaiGuest

The code is like this:

{
double xx = _x;
double yy = _y;
}

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

mihai, Mar 2, 2005
4. ### Tim LoveGuest

"mihai" <> writes:

>The code is like this:

>{
> double xx = _x;
> double yy = _y;
>}

>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
5. ### Karl Heinz BucheggerGuest

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
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
6. ### Niels DybdahlGuest

> 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
7. ### Karl Heinz BucheggerGuest

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
8. ### msaltersGuest

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