ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++中NaN异常值捕捉方法

2022-05-16 23:35:11  阅读:79  来源: 互联网

标签:尝试 val NaN C++ 捕捉 calculations 断点


问题

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

标签:尝试,val,NaN,C++,捕捉,calculations,断点
来源: https://www.cnblogs.com/azureology/p/16279071.html

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有