Velocity Reviews > C++ > need help with operations stability

# need help with operations stability

mihai
Guest
Posts: n/a

 03-02-2005
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.

Tim Love
Guest
Posts: n/a

 03-02-2005
"mihai" <(E-Mail Removed)> 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?

mihai
Guest
Posts: n/a

 03-02-2005
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.

Tim Love
Guest
Posts: n/a

 03-02-2005
"mihai" <(E-Mail Removed)> 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

Karl Heinz Buchegger
Guest
Posts: n/a

 03-02-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)

Niels Dybdahl
Guest
Posts: n/a

 03-02-2005
> 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

Karl Heinz Buchegger
Guest
Posts: n/a

 03-02-2005
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
(E-Mail Removed)

msalters
Guest
Posts: n/a

 03-02-2005

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