Visual C++ 2008 Release Build Breaking My Floats



你好,

我的项目是用Microsoft Visual C++2008速成版以发布模式构建的。然而,我在这里的一些浮点值与调试配置中的浮点值不同。出于某种原因,对于某些特定的功能,它们都变成了NaN或零。我不确定为什么会发生这种情况,这是我第一次在发布模式下构建,任何帮助都将不胜感激!

尝试的步骤:

  • 浮点命令行选项。

  • 由于几个原因,逐步完成代码操作不起作用。

  • 盯着代码看几个小时感谢您的阅读!

这是错误的代码(注意:我过去在使用具有此特定功能的NaN时遇到过问题,然而这很奇怪):

/*
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 ) ) );
}

标题:

/*
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;
};
}
}
}

上下文:

/*
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; }
};
}
}
}

注意到这些文件存在问题:

/*
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;
}

标题:

/*
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();
};
}
}
}

好。因此,正如我在评论中所写的那样,我不知道你的问题的答案,但也许我可以提供一些建议,帮助你找到问题所在。

我的第一个建议是把你的代码从lua上下文中取出,并进行一个小的单元测试来调试它。如果在发布模式下的单元测试没有错误,那么你的问题就出在其他地方了。如果仍然存在错误,您可以在代码中发现错误。如果你确信你的代码中没有错误,这里有一些指针

根据我的经验,当一个人遇到NaN时,零或INF,如果没有,并且输入或计算没有问题,那么您可能需要检查您或您使用的任何库是否以任何方式更改浮点标志(在窗口中,您使用controlfp或control87,在GCC中,您有FPU_SETCW FPU_GETCW宏)这样做可能会导致这样的问题。确保所有库都使用相同的浮点标志进行编译。

您可能还想更改检查mag是否为零的方式。浮点值接近零时,您总是会遇到麻烦。如果你不知道Bruce Dawson关于比较浮动的文章,这里有最新版本的链接:http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

如果这些都没有帮助,那么您可能正在从LUA传递坏数字。

相关内容

  • 没有找到相关文章

最新更新