need help with operations stability

M

mihai

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.
 
T

Tim Love

mihai said:
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?
 
M

mihai

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.
 
T

Tim Love

K

Karl Heinz Buchegger

mihai said:
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 '==' ?
 
N

Niels Dybdahl

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
 
K

Karl Heinz Buchegger

Niels said:
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.
 
M

msalters

Karl said:
mihai wrote:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top