views:

74

answers:

2

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?

+3  A: 

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;
GMan
A: 

If you exceed FLT_MAX then your float value will become INF and you can test for this explicitly, e.g.

#include <iostream>
#include <cfloat>
#include <cmath>

using namespace std;

int main(void)
{
    float f1 = FLT_MAX;
    float f2 = f1 * 1.001f;
    cout << "f1 = " << f1 << ", f2 = " << f2 << endl;
    cout << "isinf(f1) = " << isinf(f1) << ", isinf(f2) = " << isinf(f2) << endl;
    return 0;
}
Paul R
+/-Inf is not the same as NaN
Axel Gneiting
@Axel: good point - I shouldn't have conflated the two - I tend to think of INF as one kind of NaN but IEEE-754 treats them as different entities.
Paul R