通过自己划分浮点产生非常大的整数

时间:2012-02-01 11:17:10

标签: c++ math sfml

所以我觉得在我看来这是一个非常奇怪的问题。我有一个粗略的系统,用于在2D平面上对物体施加力,其中一个最简单的计算似乎是导致我的一个变量溢出。我有以下几行:

int ySign = m_Momentum.y / abs(m_Momentum.y);

Momentum有两个数据成员,x y(m_Momentum是一个SFML sf :: Vector2 of floats)。现在,通常公式应该总是返回1或-1,这取决于Momentum.y的符号(除非我非常错误)。

然而,它偶尔会返回极其高的数字,如-2147483648。在那种特殊情况下,m_Momentum.y的值是0.712165(两个值都是通过发送到std :: cout获得的);我再次尝试,m_Momentum.y是-0.578988,而ySign仍然是-2147483648。有一个相应的xSign有时会翻转,通常具有相同的最终值。我不能100%确认这总是结果,但目前似乎就是这种情况。

我有点难以理解为什么会这样,当它发生时,它基本上使我的程序无效(它会立即向错误的方向发送数百万像素的对象)。上面的这条线在逻辑上似乎不可能返回如此奇怪的结果。

以下是我正在处理的功能。可能是错误的做法,但我没想到会出现如此可怕的错误。它产生的打印输出显示所有数字看起来都正常,直到打印出标志;其中一个总是很庞大,之后你会看到像-2.727e + 008这样的数字(就我所知,它是科学记数法 - 即-2.727 * 10 ^ 8)。

///MODIFY MOMENTUM
//Reset, if necessary
if (Reset == true)
{
    m_Momentum.x = 0;
    m_Momentum.y = 0;
}
sf::Vector2<float> OldMoment = m_Momentum;

//Apply the force to the new momentum.
m_Momentum.x += Force.x;
m_Momentum.y += Force.y;
sf::Vector2<float> NewMoment = m_Momentum;

//Calculate total momentum.
float sqMomentum = m_Momentum.x * m_Momentum.x + m_Momentum.y * m_Momentum.y;
float tMomentum = sqrt(sqMomentum);

//Preserve signs for later use.
int xSign = m_Momentum.x / abs(m_Momentum.x);
int ySign = m_Momentum.y / abs(m_Momentum.y);

//Determine more or less the ratio of importance between x and y components
float xProp;
float yProp;
if (abs(tMomentum) > m_MaxVelocity)
{
    //Get square of maximum velocity
    int sqMax = m_MaxVelocity * m_MaxVelocity;
    //Get proportion of contribution of each direction to velocity
    xProp = (m_Momentum.x * m_Momentum.x) / sqMomentum;
    yProp = (m_Momentum.y * m_Momentum.y) / sqMomentum;
    //Reset such that the total does not exceed maximum velocity.
    m_Momentum.x = sqrt(sqMax * xProp) * xSign;
    m_Momentum.y = sqrt(sqMax * yProp) * ySign;
}

///SANITY CHECK
//Preserve old tMomentum
float tOld = tMomentum;

//Calculate current tMomentum
sqMomentum = m_Momentum.x * m_Momentum.x + m_Momentum.y * m_Momentum.y;
tMomentum = sqrt(sqMomentum);

//If it's still too high, print a report.
if (tMomentum > m_MaxVelocity)
{
    std::cout << "\n\nSANITY CHECK FAILED\n";
    std::cout << "-\n";
    std::cout << "Old Components: " << OldMoment.x << ", " << OldMoment.y << "\n";
    std::cout << "Force Components: " << Force.x << ", " << Force.y << "\n";
    std::cout << "-\n";
    std::cout << "New Components: " << NewMoment.x << ", " << NewMoment.y << "\n";
    std::cout << "Which lead to...\n";
    std::cout << "tMomentum: " << tOld << "\n";
    std::cout << "-\n";
    std::cout << "Found these proportions: " << xProp << ", " << yProp << "\n";
    std::cout << "Using these signs: " << xSign << ", " << ySign << "\n";
    std::cout << "New Components: " << m_Momentum.x << ", " << m_Momentum.y << "\n";
    std::cout << "-\n";
    std::cout << "Current Pos: " << m_RealPosition.x << ", " << m_RealPosition.y << "\n";
    std::cout << "New Pos: " << m_RealPosition.x + m_Momentum.x << ", " << m_RealPosition.y + m_Momentum.y << "\n";
    std::cout << "\n\n";
}

///APPLY FORCE
//To the object's position.
m_RealPosition.x += m_Momentum.x;
m_RealPosition.y += m_Momentum.y;

//To the sprite's position.
m_Sprite.Move(m_Momentum.x, m_Momentum.y);

有人能解释一下这里发生了什么吗?

编辑:RedX帮我指导了以下帖子:Is there a standard sign function (signum, sgn) in C/C++?这使我编写了以下代码行:

//Preserve signs for later use.
//int xSign = m_Momentum.x / abs(m_Momentum.x);
//int ySign = m_Momentum.y / abs(m_Momentum.y);
int xSign = (m_Momentum.x > 0) - (m_Momentum.x < 0);
int ySign = (m_Momentum.y > 0) - (m_Momentum.y < 0);

由于上述原因,我不再有这个奇怪的问题了。有关解释/替代解决方案,请参阅下面的Didier的帖子。

2 个答案:

答案 0 :(得分:8)

您应该使用fabs()而不是abs()来获取浮点数的绝对值。如果使用整数绝对函数,则结果为整数...

例如,-0.5 / abs(-0.5)被视为-0.5 / 0,导致负无穷大(作为浮点值)转换为int 0x80000000 = {{1的最小值}}

答案 1 :(得分:0)

将绝对值和划分声音分散为对我来说非常浪费的周期。

有什么问题
x > 0 ? 1 : -1

你可以随时放入一个函数

template <class T>
inline int sgn(const T &x) { return x > 0 ? : 1; }