How to handle a float overflow?

c++, error-handling, floating-point, overflow

Solution

You could do:

if (numeric_limits<float>::max() - val < m_speed)
{
    m_speed = 0;
}
else
{
    m_speed += val;
}

Another method might be:

m_speed += val;
if (m_speed == numeric_limits<float>::infinity())
    m_speed = 0;

But do keep in mind when an overflow actually occurs, the result is undefined behavior. So while this probably works on most machines, it isn't guaranteed. You're better of catching it before it happens.

Because this isn't trivial to read at first, I'd wrap it into a function:

template <typename T>
bool will_overflow(const T& pX, const T& pValue, 
                    const T& pMax = std::numeric_limits<T>::max())
{
    return pMax - pValue < pX;
}

template <typename T>
bool will_underflow(const T& pX, const T& pValue, 
                    const T& pMin = std::numeric_limits<T>::min())
{
    return pMin + pValue > pX;
}

m_speed = will_overflow(m_speed, val) ? 0 : m_speed + val;

Problem

If a float overflow occurs on a value, I want to set it to zero, like this... ``` m_speed += val; if ( m_speed > numeric_limits<float>::max()) { // This might not even work, since some impls will wraparound after previous line m_speed = 0.f } ``` but once `val` has been added to `m_speed`, the overflow has already occurred (and I'm assuming that the same problem would occur if i did `if (( m_speed + val ) > ..)`. How can I check to make sure an overflow is going to occur, without causing an overflow?

Original source