C++中NaN异常值捕捉方法
问题
debug代码发现某个值偶发变为-NaN
需要定位具体位置。
尝试
基本可以判断是除0引起,想当然尝试了val < -10000
的条件断点,无法捕捉。
解决
查阅文献
NaN is designed to propagate through all calculations, infecting them like a virus, so if somewhere in your deep, complex calculations you hit upon a NaN, you don't bubble out a seemingly sensible answer.
因此根据NaN
的特性,一句简单的断言即可命中:
assert(val == val);
参考
floating point - Why is NaN not equal to NaN? - Stack Overflow