|
This problem, unlike the other, is definitely not a result of my eyes/brain
being tired or something. I thoroughly displayed every value before and
after evaluation and the result is ***WRONG***. Here is the code and the
values:
typedef struct {
int magnitude;
int direction;
};
vector goal, heading;
//*** the call is sumVectors(&goal, &goal, &heading);
void sumVectors(vector* result, vector* a, vector* b)
{
long rx, ry;
// a->magnitude equals 256 (as it should) as displayed by lcd_int()
// a->direction equals 0 (as it should) as displayed by lcd_int()
// TA_costable[a->direction] = 128 (as it should) as displayed by lcd_int()
rx = (long)(a->magnitude * TA_costable[a->direction]) >> 7;
//*** THIS IS INCORRECT AND INCONCEIVABLE
// rx equals -256 as displayed by lcd_int()
//***************************************
rx += ((long)(b->magnitude * TA_costable[b->direction]) >> 7);
ry = (long)(a->magnitude * TA_sintable[a->direction]) >> 7;
ry += ((long)(b->magnitude * TA_sintable[b->direction]) >> 7);
// magnitude in 3.22mm units; direction in Turn Angle run's
result->direction = TA_RUN(iatan2(ry, rx));
result->magnitude = isqrt((rx * rx) + (ry * ry));
display("DIR",result->direction);
display("MAG",result->magnitude);
}
I have tried replacing the '>> 7' with '/ 128', substituting a->magnitude
and a->direction with int am and ad (and equating), and removing the (long)
cast while changing rx to int. No matter what I do, 256 * 128 / 128 = -256
(???). Tell me there is a fix and what it is.
Greatly Perturbed,
Robert Templeton
|
|
Message has 2 Replies: | | RE: Incorrect result
|
| <snipped code> (...) Robert, this is a classic problem with ranges of singed numbers in C rx = (long)(a->magnitude * TA_costable[a->direction]) >> 7; I don't have an RCX in frot of me, but I think you need to cast a->magnitude to long before the (...) (23 years ago, 30-Jan-02, to lugnet.robotics.rcx.legos)
| | | Re: Incorrect result
|
| (...) 256 * 128 = 32768, one higher as the biggest positive integer value. The cast to long would help, but your cast is too late. I have not tested, but I think rc += ( (long)b->magnitude * (long)TA_costable[...] ) >> 7; will help. Try output in (...) (23 years ago, 30-Jan-02, to lugnet.robotics.rcx.legos)
|
4 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|