   Function not returning correct value
  I have the following two functions in my vector class:double Vector2::Magnitude(){    return sqrt((x * x) + (y * y));}Vector2 Vector2::Normalize(){    if(!Is_Zero_Vector())    {   //Vector2 temp;   //temp.x = x / this->Magnitude();   //temp.y = y / this->Magnitude();   //x = temp.x;   //y = temp.y;   x /= this->Magnitude();   y /= this->Magnitude();   return *this;    }    return *this;}My test vector is initialized as (12.0,-5.0) and when I call the Normalize function it returns x = 0.923, and y = -0.983. The x value is correct but the y value should be -0.384. If I use the code that is commented out instead I get the correct results for both values. Anyone know why the y value is not returning what it should?   
Quote:Original post by TheBinaryManI have the following two functions in my vector class:*** Source Snippet Removed ***My test vector is initialized as (12.0,-5.0) and when I call the Normalize function it returns x = 0.923, and y = -0.983. The x value is correct but the y value should be -0.384. If I use the code that is commented out instead I get the correct results for both values. Anyone know why the y value is not returning what it should?After you change the value of x, the call to magnitude returns a different value than the first time, so you should do something like:double mag = this->Magnitude();this->x /= mag;this->y /= mag;;  Thanks that did solve it, slipped right past me that x was being changed, doh! Thanks.   ;  This is a really good example of why you should always try to avoid multiple calls to the same function by creating a local variable. In this case, not only is it twice as slow as it should be, it also introduced a bug that gave incorrect results.   ;  While we are at it, you are returning a copy of a Vector2, but you also change the contents of the Vector2 it was called on.I'd say, from a design point of view, either return a Vector2 and leave the original untouched or return void and change the original. But don't combine them into one.Does that make sense?class Vector2{public:    Vector2( float x, float y )    :    m_x( x ),    m_y( y )    {    }    float Magnitude()    {   return sqrtf( (m_x * m_x) + (m_y * m_y) );    }    void Normalize()    {   float length = Magnitude();   m_x /= length;   m_y /= length;    }    friend Vector2 Normalize( const Vector2& rhs )    {   Vector2 returnVector = rhs;   returnVector.Normalize();   return returnVector;    }private:    float m_x;    float m_y;};int main(){    Vector2 xAxis( 2.0f, 0.0f );    xAxis.Normalize();    Vector2 yAxis( 0.0f, 2.0f );    Vector2 normalizedYAxis = Normalize( yAxis );    // at this point in time:    // xAxis is normalized    // yAxis didn't change    // normalizedYAxis is the normalized version of yAxis    return 0;} [Edited by - Dolf on May 24, 2009 5:13:29 AM];  I changed the value of the vector and returned it just in case I ever wanted to assign it to another vector when I normalized the first vector.vect2 = vect1.Nomralize();I don't know if there's any difference in performance between the two, but thats the reason I did it that way.   ;  The point I think Dolf was making is that it is unintuitive.If I see:someVector.normalise();Then I am going to assume normalise() modifys the object it is called on.However, if I see:Vector otherVector = someVector.normalise();I am going to assume that "someVector" is not modified. This can lead to bugs.You know your own codebase and conventions, but to other programmers this seems strange at first.   