سؤال

Good day,

I got my project to build In release mode with Microsoft Visual C++ 2008 Express Edition. However some of my floating point values up here to be different than those in the debug configuration. For some reason they all are turning out to be NaN or zero for some particular functionality. I am unsure why this is happening and this is my first time building in release mode any help is appreciated!

Attempted Steps:

  • Floating point command line options.

  • Stepping through the code this did not work for a few reasons.

  • Hours of staring at the code Thank you for reading!

Here Is The Misbehaving Code (Note: I have had trouble with NaNs with this particular functionality in the past, however this is just strange):

    /*
This file is part of White - Storm: Lightning (alpha).

    Copyright 2012 Christopher Augustus Greeley

    White - Storm: Lightning (alpha) is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    White - Storm: Lightning (alpha) is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with White - Storm: Lightning (alpha).  If not, see <http://www.gnu.org/licenses/>.
*/
#include "Vector Calculator.h"
WSL::Math::Vector::VectorCalculator::VectorCalculator()
{
    WSL::Containers::Base::XYZ temp;
    default_ = temp;
}
WSL::Containers::Base::XYZ WSL::Math::Vector::VectorCalculator::VectorCalculation( WSL::Containers::Base::XYZ goTo, WSL::Containers::Base::XYZ position, float speed, bool td )
{
    //Make sure that the vector doesent have any stray values lying around.//
    vector = default_;
    //Calculate Magnitude.//
    x = goTo.getX() - position.getX();
    y = goTo.getY() - position.getY();
    z = goTo.getZ() - position.getZ();
    if( td == true )
        magnitude = magn.DotProduct( x, y, z, magnitude );
    else
        magnitude = magn.DotProduct( x, y, magnitude );
    if( x == 0 && y == 0 && z == 0 )
        return vector;
    //Normilise//
    magnitude = sqrt( magnitude );
    //Make sure we do not divide by zero.//
    if( magnitude != 0 )
    {
        if( x != 0 )
            x /= magnitude;
        if( y != 0 )    
            y /= magnitude;
        if( td == true )
            if( z != 0 )
                z /= magnitude;
    }
    //Go The Desired Speed.//
    if( speed >=0 )
    {
        x *= speed;
        y *= speed;
    }
    if( td == true )
        z *= speed;
    vector.setX( x );
    vector.setY( y );
    vector.setZ( z );
    return vector;
}
inline float WSL::Math::Formulas::Dot::DotProduct( float x, float y, float mag )
{
    return ( mag = ( ( x ) * ( x ) + ( y ) * ( y ) ) );
}
inline float WSL::Math::Formulas::Dot::DotProduct( float x, float y, float z, float mag )
{
    return ( mag = ( ( x ) * ( x ) + ( y ) * ( y ) + ( z ) * ( z ) ) );
}

Header:

    /*
This file is part of White - Storm: Lightning (alpha).

    Copyright 2012 Christopher Augustus Greeley

    White - Storm: Lightning (alpha) is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    White - Storm: Lightning (alpha) is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with White - Storm: Lightning (alpha).  If not, see <http://www.gnu.org/licenses/>.
*/
#include "Int Bool.h"
namespace WSL
{
    namespace Math
    {
        namespace Formulas
        {
            class Dot
            {
            public:
                inline float DotProduct( float x, float y, float mag );
                inline float DotProduct( float x, float y, float z, float mag );
            };
        }
        namespace Vector
        {
            struct VectorCalculator
            {
                VectorCalculator();
                WSL::Containers::Base::XYZ VectorCalculation( WSL::Containers::Base::XYZ goTo, WSL::Containers::Base::XYZ position, float speed, bool td );
                WSL::Containers::Base::XYZ VectorCalculation( WSL::Containers::Base::XYZ goTo, WSL::Containers::Base::XYZ *position, float speed, bool td );
                private:
                    WSL::Containers::Base::XYZ vector;
                    WSL::Containers::Base::XYZ default_;
                    float x, y, z, magnitude;
                    Math::Formulas::Dot magn;
            };
        }
    }
}

For Context:

/*
This file is part of White - Storm: Lightning (alpha).

    Copyright 2012 Christopher Augustus Greeley

    White - Storm: Lightning (alpha) is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    White - Storm: Lightning (alpha) is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with White - Storm: Lightning (alpha).  If not, see <http://www.gnu.org/licenses/>.
*/
#include "Include.h"
namespace WSL
{
    namespace Containers
    {
        namespace Base
        {
            struct XYZB
            {
                inline float getX() { return X; }
                inline float getY() { return Y; }
                inline float getZ() { return Z; }
                inline void setX( float Value ) { X = Value; }
                inline void setY( float Value ) { Y = Value; }
                inline void setZ( float Value ) { Z = Value; }
                protected:
                    float X, Y, Z;
            };
            struct XYZ : public XYZB
            {
            public:
                XYZ( float x, float y, float z )
                {
                    X = x;
                    Y = y; 
                    Z = z;
                }
                inline XYZ() { X = 0; Y = 0; Z = 0; }
            };
        }
    }
}

The problem was noticed with these files:

/*
This file is part of White - Storm: Lightning (alpha).

    Copyright 2012 Christopher Augustus Greeley

    White - Storm: Lightning (alpha) is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    White - Storm: Lightning (alpha) is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with White - Storm: Lightning (alpha).  If not, see <http://www.gnu.org/licenses/>.
*/
#include "Vector.h"
WSL::Containers::Math::Vector::Vector()
{
    destinationInitialize = false;
    threeDimentional = false;
}
WSL::Containers::Math::Vector::Vector( WSL::Containers::Base::XYZ position_ )
{
    position = position_;
    destinationInitialize = false;
    threeDimentional = false;
}
WSL::Containers::Math::Vector::Vector( WSL::Containers::Base::XYZ position_, bool threeDimentional_ )
{
    position = position_;
    destinationInitialize = false;
    threeDimentional = threeDimentional_;
}
float WSL::Containers::Math::Vector::GetDestinationX()
{
    return destination.getX();
}
float WSL::Containers::Math::Vector::GetDestinationY()
{
    return destination.getY();
}
float WSL::Containers::Math::Vector::GetDestinationZ()
{
    return destination.getZ();
}
WSL::Containers::Base::XYZ WSL::Containers::Math::Vector::GetDestination()
{
    return destination;
}
WSL::Containers::Base::XYZ WSL::Containers::Math::Vector::GetPosition()
{
    return position;
}
float WSL::Containers::Math::Vector::GetX()
{
    return position.getX();
}
float WSL::Containers::Math::Vector::GetY()
{
    return position.getY();
}
float WSL::Containers::Math::Vector::GetZ()
{
    return position.getZ();
}
void WSL::Containers::Math::Vector::SetThreeDimentional( bool value )
{
    threeDimentional = value;
}
bool WSL::Containers::Math::Vector::GetThreeDimentional()
{
    return threeDimentional;
}
void WSL::Containers::Math::Vector::CalculateVector()
{
    vector = vectorCalculator.VectorCalculation( destination, position, speed, threeDimentional );
}
void WSL::Containers::Math::Vector::Move()
{
    position.setX( position.getX() + vector.getX() );
    position.setY( position.getY() + vector.getY() );
    position.setZ( position.getZ() + vector.getZ() );
}
void WSL::Containers::Math::Vector::SetPosition( WSL::Containers::Base::XYZ position_ )
{
    position = position_;
}
void WSL::Containers::Math::Vector::SetSpeed( float speed_ )
{
    speed = speed_;
}
void WSL::Containers::Math::Vector::SetDestination( float x, float y )
{
    destination.setX( x );
    destination.setY( y );
    if( destinationInitialize == false )
    {
        destinationInitialize = true;
        destination.setZ( 0 );
    }
}
void WSL::Containers::Math::Vector::SetDestination( float x, float y, float z )
{
    destination.setX( x );
    destination.setY( y );
    destination.setZ( z );
}
void WSL::Containers::Math::Vector::SetDestination( WSL::Containers::Base::XYZ destination_ )
{
    destination = destination_;
}
void WSL::Containers::Math::Vector::SetDestination( float allCoords )
{
    destination.setX( allCoords );
    destination.setY( allCoords );
    destination.setZ( allCoords );
}
void WSL::Containers::Math::Vector::SetVector( WSL::Containers::Base::XYZ vector_ )
{
    vector = vector_;
}
WSL::Containers::Base::XYZ WSL::Containers::Math::Vector::GetVector()
{
    return vector;
}
float WSL::Containers::Math::Vector::GetSpeed()
{
    return speed;
}

Header:

/*
This file is part of White - Storm: Lightning (alpha).

    Copyright 2012 Christopher Augustus Greeley

    White - Storm: Lightning (alpha) is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    White - Storm: Lightning (alpha) is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with White - Storm: Lightning (alpha).  If not, see <http://www.gnu.org/licenses/>.
*/
#include "Engine.h"
namespace WSL
{
    namespace Containers
    {
        namespace Math
        {
            class Vector
            {
                WSL::Math::Vector::VectorCalculator vectorCalculator;
                WSL::Containers::Base::XYZ position;
                WSL::Containers::Base::XYZ destination;
                WSL::Containers::Base::XYZ vector;
                bool destinationInitialize, threeDimentional;
                float speed;
            public:
                Vector();
                Vector( WSL::Containers::Base::XYZ position_ );
                Vector( WSL::Containers::Base::XYZ position_, bool threeDimentional_ );
                void CalculateVector();
                bool GetThreeDimentional();
                void Move();
                void SetPosition( WSL::Containers::Base::XYZ position_ );
                void SetDestination( float x, float y );
                void SetDestination( float x, float y, float z );
                void SetDestination( WSL::Containers::Base::XYZ destination_ );
                void SetDestination( float allCoords );
                void SetSpeed( float speed_ );
                void SetThreeDimentional( bool value );
                void SetVector( WSL::Containers::Base::XYZ vector_ );
                float GetDestinationX();
                float GetDestinationY();
                float GetDestinationZ();
                WSL::Containers::Base::XYZ GetDestination();
                WSL::Containers::Base::XYZ GetPosition();
                WSL::Containers::Base::XYZ GetVector();
                float GetX();
                float GetY();
                float GetZ();
                float GetSpeed();
            };
        }
    }
}
هل كانت مفيدة؟

المحلول

OK. So as I wrote in the comments I don't know the answer to to your problem but maybe I can provide some pointers that may help you find the problem.

My first suggestion would be to take out your code out of the lua context and make a small unit test for debugging it. If there are no errors with the unit test in release mode then your problem is elsewhere. If there are still errors you may be able to find if you have a bug in your code. If you are certain you don't have a bug in your code here are some pointers

In my experience when one encounters NaNs, Zeros or INFs where none are expected and there are no problems with the input or the calculations then you might want to check if you or any of the libraries you use changes the floating point flags in any way (In windows you use controlfp or control87 for that, with GCC you have FPU_SETCW FPU_GETCW macros) Also mismatching compilation flags that alter the way floating point calculations are done may cause such problems. Make sure that all libraries are compiled using the same floating point flags.

You may also want to change the way you check that mag is not ZERO. You can always get into trouble with floating point values near ZERO. If you don't know Bruce Dawson's article on comparing floats here is a link to the latest version: http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

If none of this helps you are probably passing bad number from LUA.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top