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

Gar*_*ckW 3 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);
Run Code Online (Sandbox Code Playgroud)

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

编辑:RedX帮我指导了以下帖子:C/C++中是否有标准符号函数(signum,sgn)?这让我编写了以下几行代码:

//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);
Run Code Online (Sandbox Code Playgroud)

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

Did*_*set 8

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

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

  • 或者`std :: abs`,因为这是C++. (4认同)
  • 这为启用警告并关注它们的做法提供了一个很好的例子.对我来说,我发现使用最严格的警告级别,并将警告视为编译错误,可以节省比成本更多的时间. (2认同)