Miracusly incorrect calculations

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Till Crueger

    Miracusly incorrect calculations

    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::operato r-(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=iP layerPos - 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
    --
    Please add "Salt and Peper" to the subject line to bypass my spam filter

  • John Harrison

    #2
    Re: Miracusly incorrect calculations


    "Till Crueger" <TillFC@gmx.net > wrote in message
    news:c739is$tu0 $1@f1node01.rhr z.uni-bonn.de...[color=blue]
    > 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::operato r-(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=iP layerPos - 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
    > --[/color]

    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


    John


    Comment

    • Till Crueger

      #3
      Re: Miracusly incorrect calculations

      On Sun, 02 May 2004 18:11:27 +0100, John Harrison wrote:
      [color=blue]
      > Please post complete, compilable code, its the quickest way to get
      > answers.[/color]

      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::operato r-(vector b) {
      vector result = vector(xVal-b.x(),yVal-b.y()); return result;
      }

      File Myclass.cpp:

      #define PLAYER_SPEED 1

      robvec estimatePos(Bal l* 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

      --
      Please add "Salt and Peper" to the subject line to bypass my spam filter

      Comment

      • John Harrison

        #4
        Re: Miracusly incorrect calculations


        "Till Crueger" <TillFC@gmx.net > wrote in message
        news:c73ctr$11o m$1@f1node01.rh rz.uni-bonn.de...[color=blue]
        > On Sun, 02 May 2004 18:11:27 +0100, John Harrison wrote:
        >[color=green]
        > > Please post complete, compilable code, its the quickest way to get
        > > answers.[/color]
        >
        > 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::operato r-(vector b) {
        > vector result = vector(xVal-b.x(),yVal-b.y()); return result;
        > }
        >
        > File Myclass.cpp:
        >
        > #define PLAYER_SPEED 1
        >
        > robvec estimatePos(Bal l* 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[/color]

        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


        Comment

        • Leor Zolman

          #5
          Re: Miracusly incorrect calculations

          On Sun, 02 May 2004 18:58:44 +0200, "Till Crueger" <TillFC@gmx.net > wrote:
          [color=blue]
          >On Sun, 02 May 2004 18:11:27 +0100, John Harrison wrote:
          >[color=green]
          >> Please post complete, compilable code, its the quickest way to get
          >> answers.[/color]
          >
          >Ok, here is some more of the code[/color]

          [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

          --
          Leor Zolman --- BD Software --- www.bdsoft.com
          On-Site Training in C/C++, Java, Perl and Unix
          C++ users: download BD Software's free STL Error Message Decryptor at:
          An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

          Comment

          • Gary Labowitz

            #6
            Re: Miracusly incorrect calculations

            "Leor Zolman" <leor@bdsoft.co m> wrote in message
            news:0tfa901ie7 oj09b1j4gliebv0 l9pk56n7r@4ax.c om...[color=blue]
            > On Sun, 02 May 2004 18:58:44 +0200, "Till Crueger" <TillFC@gmx.net >[/color]
            wrote:[color=blue]
            >[color=green]
            > >On Sun, 02 May 2004 18:11:27 +0100, John Harrison wrote:
            > >[color=darkred]
            > >> Please post complete, compilable code, its the quickest way to[/color][/color][/color]
            get[color=blue][color=green][color=darkred]
            > >> answers.[/color]
            > >
            > >Ok, here is some more of the code[/color]
            >
            > [snip]
            >
            > [John just posted to say basically the same thing, but I'll go ahead[/color]
            and[color=blue]
            > send this anyway, since I've typed it up ;-) ][/color]

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


            Comment

            • Dave Moore

              #7
              Re: Miracusly incorrect calculations

              "Gary Labowitz" <glabowitz@comc ast.net> wrote in message news:<XtednWPGJ qBB5AjdRVn-sQ@comcast.com> ...[color=blue]
              > "Leor Zolman" <leor@bdsoft.co m> wrote in message
              > news:0tfa901ie7 oj09b1j4gliebv0 l9pk56n7r@4ax.c om...[color=green]
              > > On Sun, 02 May 2004 18:58:44 +0200, "Till Crueger" <TillFC@gmx.net >[/color]
              > wrote:[color=green]
              > >[color=darkred]
              > > >On Sun, 02 May 2004 18:11:27 +0100, John Harrison wrote:
              > > >
              > > >> Please post complete, compilable code, its the quickest way to[/color][/color]
              > get[color=green][color=darkred]
              > > >> answers.
              > > >
              > > >Ok, here is some more of the code[/color]
              > >
              > > [snip]
              > >
              > > [John just posted to say basically the same thing, but I'll go ahead[/color]
              > and[color=green]
              > > send this anyway, since I've typed it up ;-) ][/color]
              >
              > 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.[/color]

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

              Comment

              • Howard

                #8
                Re: Miracusly incorrect calculations


                "Till Crueger" <TillFC@gmx.net > wrote in message
                news:c73ctr$11o m$1@f1node01.rh rz.uni-bonn.de...[color=blue]
                > On Sun, 02 May 2004 18:11:27 +0100, John Harrison wrote:
                >[color=green]
                > > Please post complete, compilable code, its the quickest way to get
                > > answers.[/color]
                >
                > 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::operato r-(vector b) {
                > vector result = vector(xVal-b.x(),yVal-b.y()); return result;
                > }[/color]

                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.)
                [color=blue]
                >
                > File Myclass.cpp:
                >
                > #define PLAYER_SPEED 1
                >
                > robvec estimatePos(Bal l* ball) const {
                > int resScore=0;
                > robvec resPos;[/color]

                What's a robvec?
                [color=blue]
                > 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[/color]

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

                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).
                [color=blue]
                > 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;[/color]

                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.
                [color=blue]
                > }
                >
                > 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
                >
                > --
                > Please add "Salt and Peper" to the subject line to bypass my spam filter
                >[/color]


                Comment

                • Mark A. Gibbs

                  #9
                  Re: Miracusly incorrect calculations


                  Dave Moore wrote:
                  [color=blue][color=green]
                  >>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.[/color]
                  >
                  >
                  > Lol ... put me down for "indexing past the end of an array".[/color]

                  i'll go for stack overflow. i always bet on the long shot.

                  mark

                  Comment

                  Working...