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?