Miracusly incorrect calculations

T

Till Crueger

Hi,
I have the folowing code (Assuming corect constructors and accesors):

class Vector{
private:
double x;
double y;
public:
Vector operator-(Vector b);
}

Vector Vector::eek:perator-(Vector b){
Vector result= Vector(x-b.x(),y-b.y());
return result;
}

in another method I do the following:

set Vector iBallPos to some value
set Vector iPlayerPos to some value

Vector iBallDistVec=iPlayerPos - iBallPos;

when output IBallPos, iPlayerPos and iBallDist I get the following:

Token "iBallPos.x()" is: 45.4729
Token "iBallPos.y()" is: -19.3021
Token "iPlayerPos.x()" is: 0
Token "iPlayerPos.y()" is: 0
Token "iBallDistVec.x()" is: -0.920505
Token "iBallDistVec.y()" is: 0.390731

or similar wrong values.
If anybody has any idea, what is wrong wrong with this code, please let me
know.
Till
 
T

Till Crueger

Please post complete, compilable code, its the quickest way to get
answers.

Ok, here is some more of the code

File vector.h:

class vector {
private:
double xVal;
double yVal;
public:
vector();
vector(double _x, double _y);
double x();
double y();
double l();
vector operator-(vector b);
};

File Vector.cpp:

#include "vector.h"

vector::vector() {
xVal = 0;
yVal = 0;
}

vector::vector(double _x, double _y) {
xVal = _x;
yVal = _y;
}

double vector::x() {
return xVal;
}


double vector::y() {
return yVal;
}

double vector::l() {
double _l = sqrt(xVal*xVal+yVal*yVal);
return _l;
}

vector vector::eek:perator-(vector b) {
vector result = vector(xVal-b.x(),yVal-b.y()); return result;
}

File Myclass.cpp:

#define PLAYER_SPEED 1

robvec estimatePos(Ball* ball) const {
int resScore=0;
robvec resPos;
const int steps = 20;

for(int i=0;i<steps;i++){

int score=steps*2-i*2;

//some calculations which don't seem to work
vector iBallPos = ball->relPos()
+ i* ball->relVel();
vector iPlayerPos = robvec(0,0)
+ iBallPos* PLAYER_SPEED*i;
vector iBallDistVec = iPlayerPos - iBallPos;

double iBallDist= iBallDistVec.l();
score+= 60 - iBallDist/(60.0/100.0);
if(score > resScore){
resScore=score;
resPos=iBallPos;
}
}
return resPos;
}

The ball object is a simple object that has a position, which can be
accessed throug the relPos method. The calculation in the lines below the
comment all seem to give wrong results. So I added some couts to see what
the calculations would give me, and got the Lines I posted in the post
before this one:

Token "iBallPos.x()" is: 45.4729
Token "iBallPos.y()" is: -19.3021
Token "iPlayerPos.x()" is: 0
Token "iPlayerPos.y()" is: 0
Token "iBallDistVec.x()" is: -0.920505
Token "iBallDistVec.y()" is: 0.390731

I hope this is enough code this time. If there still isn't any errors in
this code, I am totaly clueless where they could be, since this is all
that I can think of.
Thanks
Till
 
J

John Harrison

Till Crueger said:
Hi,
I have the folowing code (Assuming corect constructors and accesors):

class Vector{
private:
double x;
double y;
public:
Vector operator-(Vector b);
}

Vector Vector::eek:perator-(Vector b){
Vector result= Vector(x-b.x(),y-b.y());
return result;
}

in another method I do the following:

set Vector iBallPos to some value
set Vector iPlayerPos to some value

Vector iBallDistVec=iPlayerPos - iBallPos;

when output IBallPos, iPlayerPos and iBallDist I get the following:

Token "iBallPos.x()" is: 45.4729
Token "iBallPos.y()" is: -19.3021
Token "iPlayerPos.x()" is: 0
Token "iPlayerPos.y()" is: 0
Token "iBallDistVec.x()" is: -0.920505
Token "iBallDistVec.y()" is: 0.390731

or similar wrong values.
If anybody has any idea, what is wrong wrong with this code, please let me
know.
Till
--

It's amazing how often this happens. There clearly isn't anything wrong with
the code you've posted (there's a few improvements could be made but no
actual errors). Since I don't believe in miracles I'm sure you've made a
mistake somewhere in the code you haven't posted. Obviously I have no idea
what that is.

Please post complete, compilable code, its the quickest way to get answers.

Please read the FAQ on posting code
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

John
 
J

John Harrison

Till Crueger said:
Ok, here is some more of the code

File vector.h:

class vector {
private:
double xVal;
double yVal;
public:
vector();
vector(double _x, double _y);
double x();
double y();
double l();
vector operator-(vector b);
};

File Vector.cpp:

#include "vector.h"

vector::vector() {
xVal = 0;
yVal = 0;
}

vector::vector(double _x, double _y) {
xVal = _x;
yVal = _y;
}

double vector::x() {
return xVal;
}


double vector::y() {
return yVal;
}

double vector::l() {
double _l = sqrt(xVal*xVal+yVal*yVal);
return _l;
}

vector vector::eek:perator-(vector b) {
vector result = vector(xVal-b.x(),yVal-b.y()); return result;
}

File Myclass.cpp:

#define PLAYER_SPEED 1

robvec estimatePos(Ball* ball) const {
int resScore=0;
robvec resPos;
const int steps = 20;

for(int i=0;i<steps;i++){

int score=steps*2-i*2;

//some calculations which don't seem to work
vector iBallPos = ball->relPos()
+ i* ball->relVel();
vector iPlayerPos = robvec(0,0)
+ iBallPos* PLAYER_SPEED*i;
vector iBallDistVec = iPlayerPos - iBallPos;

double iBallDist= iBallDistVec.l();
score+= 60 - iBallDist/(60.0/100.0);
if(score > resScore){
resScore=score;
resPos=iBallPos;
}
}
return resPos;
}

The ball object is a simple object that has a position, which can be
accessed throug the relPos method. The calculation in the lines below the
comment all seem to give wrong results. So I added some couts to see what
the calculations would give me, and got the Lines I posted in the post
before this one:

Token "iBallPos.x()" is: 45.4729
Token "iBallPos.y()" is: -19.3021
Token "iPlayerPos.x()" is: 0
Token "iPlayerPos.y()" is: 0
Token "iBallDistVec.x()" is: -0.920505
Token "iBallDistVec.y()" is: 0.390731

I hope this is enough code this time. If there still isn't any errors in
this code, I am totaly clueless where they could be, since this is all
that I can think of.
Thanks
Till

Well I can't see anything wrong either. But you've made a mistake somewhere.

If you are serious about getting this fixed then you will post a *complete*
program. That doesn't not mean you post the program you have now, you have
to do some more work than that. What you do is take the existing (presumably
quite large) program and start cutting out the code that is not relevant to
the error. Repeat this until you have a small program which you can post in
its *entirety* here. Now this process is tedious but it is guaranteed to
work, either you will get down to a small program that you can post here, or
you will remove some piece of code, and suddenly the program will start
behaving. If that happens then the last piece of code that you removed is
very likely the one containing the error.

The key about this process is that it is very mechanical, you don't have to
be creative or clever, you just have to go through the slog of removing code
bit by bit. For instance you could start by removing every function except
estimatePos and call that from main with just the minimum number of objects
necessary to make the call.

I can guarantee that when the bug is found it will be something quite
different from what you expected. For instance have you double checked the
code that prints out the wrong values?

john
 
L

Leor Zolman

Ok, here is some more of the code

[snip]

[John just posted to say basically the same thing, but I'll go ahead and
send this anyway, since I've typed it up ;-) ]

Debugging code like this is a /lot/ easier, both for you and for folks who
frequent this list and like to help, if you post a complete, compilable,
running program. What is "robvec"? Are we supposed to guess that it is
equivalent to your vector? What is a Ball? Are there includes missing or
have you just accidentally re-ordered stuff? Etc. etc.

There may be enough info in what you've shown to figure out the problem, or
there may not be. But when we have to make assumptions about this or that,
it takes time /and/ blurs the picture. Results being "way off" are usually
the sign of a simple bug; you may very well detect it yourself in the
process of producing a minimal program that manifests the problem. If you
create something that compiles and you still don't see the problem, please
post that full program.
-leor
 
G

Gary Labowitz

Leor Zolman said:
Ok, here is some more of the code

[snip]

[John just posted to say basically the same thing, but I'll go ahead and
send this anyway, since I've typed it up ;-) ]

We could make this an interesting "lottery" to see who could guess
what it will be.
I'll guess it is an uninitialized variable in, say, some object.
 
D

Dave Moore

Gary Labowitz said:
Leor Zolman said:
On Sun, 02 May 2004 18:11:27 +0100, John Harrison wrote:

Please post complete, compilable code, its the quickest way to get
answers.

Ok, here is some more of the code

[snip]

[John just posted to say basically the same thing, but I'll go ahead and
send this anyway, since I've typed it up ;-) ]

We could make this an interesting "lottery" to see who could guess
what it will be.
I'll guess it is an uninitialized variable in, say, some object.

Lol ... put me down for "indexing past the end of an array".
 
H

Howard

Till Crueger said:
Ok, here is some more of the code

File vector.h:

class vector {
private:
double xVal;
double yVal;
public:
vector();
vector(double _x, double _y);
double x();
double y();
double l();
vector operator-(vector b);
};

File Vector.cpp:

#include "vector.h"

vector::vector() {
xVal = 0;
yVal = 0;
}

vector::vector(double _x, double _y) {
xVal = _x;
yVal = _y;
}

double vector::x() {
return xVal;
}


double vector::y() {
return yVal;
}

double vector::l() {
double _l = sqrt(xVal*xVal+yVal*yVal);
return _l;
}

vector vector::eek:perator-(vector b) {
vector result = vector(xVal-b.x(),yVal-b.y()); return result;
}

Since you're not changing the vector b here, you should have "const vector&
b" instead of just "vector b" as the parameter here. That avoids copying
the object contents, and also makes it clear that the function does not
modify b.

(By the way, you could just say "return vector(xVal-b.x(),yVal-b.y());". No
need to assign the temporary to a local variable and then return a copy of
that.)
File Myclass.cpp:

#define PLAYER_SPEED 1

robvec estimatePos(Ball* ball) const {
int resScore=0;
robvec resPos;

What's a robvec?
const int steps = 20;

for(int i=0;i<steps;i++){

int score=steps*2-i*2;

//some calculations which don't seem to work

Try debugging instead of just printing out values. See what happens at each
step, especially inside the - operator call.
vector iBallPos = ball->relPos()
+ i* ball->relVel();

Where's your operator +? Your * operator? Your = operator?
What are relPos and relVel?
You're assigning to a vector, but it looks like you're calculating a single
value here (unless there are + and * and = operators you haven't shown).
vector iPlayerPos = robvec(0,0)
+ iBallPos* PLAYER_SPEED*i;
vector iBallDistVec = iPlayerPos - iBallPos;

double iBallDist= iBallDistVec.l();
score+= 60 - iBallDist/(60.0/100.0);
if(score > resScore){
resScore=score;
resPos=iBallPos;
}
}
return resPos;

If your robvec object contains other objects (as it looks like it does),
then you'll probably need to define an assignment operator and a copy
constructor (and a destructor, for completeness) in the robvec class, in
order to handle things like this, where you're returning a copy of a local
robvec object.
 

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

Similar Threads

Lexical Analysis on C++ 1
TF-IDF 2
memory leak problem 0
memory leak problem 2
memory leak problem 0
Programming math challenge gives wrong answer 2
Crossword 2
discards qualifiers 2

Members online

Forum statistics

Threads
474,169
Messages
2,570,916
Members
47,458
Latest member
Chris#

Latest Threads

Top