描述
一般来说,在写代码时突然出现bug,突然提示segmentation fault
调试一下原因:我自己开发代码时,反正出现过好多次由于inf和nan带来的bug,所以在下面写一段代码来判断
inf产生的一种情况: 1.0/0.0
nan产生的一种情况:0.0/0.0
这里不是失败代码的总结博客,因此不列举它们出现的原因,只列出屏蔽它们的方法
代码
#include <iostream>
#include <math.h>
int main(int argc, char** argv) {
double x = 1.0/0.0;
double y = -x;
std::cout<<"x is "<<x<<", is inf? "<<isinf(x)<<std::endl;
std::cout<<"y is "<<y<<", is inf? "<<isinf(y)<<std::endl;
double a = 0.0/0.0;
double b = -a;
std::cout<<"a is "<<a<<", is nan? "<<isnan(a)<<std::endl;
std::cout<<"b is "<<b<<", is nan? "<<isnan(b)<<std::endl;
return 1;
}