【发布时间】:2023-03-04 01:23:01
【问题描述】:
如果一个值发生浮点溢出,我想将它设置为零,像这样......
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
}
但是一旦将val 添加到m_speed,就已经发生了溢出(我假设如果我添加if (( m_speed + val ) > ..) 也会出现同样的问题。
如何检查以确保将发生溢出而不导致溢出?
【问题讨论】:
标签: c++ error-handling floating-point overflow