【问题标题】:Dividing a Float by Itself Produces Very Large Integers将浮点数除以本身会产生非常大的整数
【发布时间】:2012-02-24 01:50:35
【问题描述】:

所以我遇到了一个在我看来非常奇怪的问题。我有一个粗略的系统来对 2D 平面上的对象施加力,最简单的计算之一似乎导致我的变量之一溢出。我有以下行:

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

其中 Momentum 有两个数据成员,xy(m_Momentum 是浮点数的 SFML sf::Vector2)。现在,通常公式应始终返回 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 的帖子。

【问题讨论】:

  • 如果只是乘法,您应该能够在 10 行以下生成一个独立的测试用例。
  • 看起来您正试图从向量分量中获取符号(或方向 (+1/-1) 在这种情况下?如果是,请参阅此线程 stackoverflow.com/questions/1903954/… 您还应该阅读这些:encrypted.google.com/…floating-point-gui.de
  • @RedX 非常感谢!我使用了顶部响应的代码,我不再有问题。我将在我的帖子底部添加更好的选项,以供将来参考。尽管如此,我还是很想知道为什么我的代码表现得很奇怪。将来可能是有用的知识。
  • 考虑使用copysignf(1.0, value),来自:standard sign function

标签: c++ math sfml


【解决方案1】:

您应该使用fabs() 而不是abs() 来获取浮点数的绝对值。如果你使用整数绝对函数,那么结果就是一个整数……

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

【讨论】:

  • std::abs,因为这是C++。
  • 啊,这可以解释这个问题。谢谢你的解释!以后我会牢记这一点。
  • 这为启用警告并注意它们的做法提供了一个非常好的示例。对我来说,我发现使用最严格的警告级别,并将警告视为编译错误,节省的时间远远多于成本。
【解决方案2】:

对我来说,采用绝对值和除法听起来非常浪费周期。怎么了

x > 0 ? 1 : -1

你总是可以把它放在一个函数中

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 2019-05-16
    相关资源
    最近更新 更多