C++ 判断浮点数是否为Nan值

2021-02-11 18:18:01 浏览数 (1)

参考链接: C Nan()

NaN means “not a number,” and is used for floating point operations. 

There are lots of floating point operations that don’t make sense, such as dividing by zero, taking the log of zero or a negative number, taking the square root of a negative number, etc. Depending on your compiler, some of these operations may produce special floating point values such as infinity (with distinct values for positive vs. negative infinity) and the not a number value, NaN. 

If your compiler produces a NaN, it has the unusual property that it is not equal to any value, including itself. For example, if a is NaN, then a == a is false. In fact, if a is NaN, then a will be neither less than, equal to, nor greater than any value including itself. In other words, regardless of the value of b, a < b, a <= b, a > b, a >= b, and a == b will all return false. 

 inline bool my_isnan(double x)

{

  return x != x;

}

0 人点赞