c ++ on the "NAN"

reference

"NaN" distress - Flying Fish World - CSDN blog
NaN - Wikipedia
NaN in C ++ - How to the What IS IT and IT for the Check -? GeeksforGeeks

Foreword

NaN ( "Not a Number") is a special floating point type , the IEEE754 when introduced in 1985, for the definition of some undefined results generated by floating point operations. NaN different and inf or -inf, it is not a particular floating-point value, it is one kind of undefined results generated by floating point operation, it can be understood as another subject, which are currently in the introduction of many languages this definition, such as python, matlab like. Of course, in the calculation of the time, usually rarely met NaN, but the event will cause abnormal results, and is accompanied in many iteration or calculation. Because, many languages NaN regarded as one data type to handle, and will not appear when an error or an exception, this is a time bomb program, especially those related to the program will be a lot of iterative matrix calculation . Someone ate the bitterness, when calling stochastic gradient descent usually requires iterative calculations will make a lot of convergence. When there is NaN, there is a very obvious feature matrix computation speed will be slower than usual 10-100 times, if your program suddenly encountered such a situation before returning to check it carefully at those places will introduce a NaN . Here are several operations occur NaN

Will return NaN has the following three operations:

  • At least one of the operands is a NaN operations
  • Undefined
    • The following division: 0/0, ∞ / ∞, ∞ / -∞, -∞ / ∞, -∞ / -∞
    • The following multiplication: 0 × ∞, 0 × -∞
    • Adding the following: ∞ + (-∞), (- ∞) + ∞
    • Following subtraction: ∞ - (-∞), (- ∞) - ∞
  • Generating a plurality of real number calculation results. E.g:
    • Square root of a negative number
    • On the negative logarithmic operation
    • Comparative +1 -1 smaller or larger than the number of performing the arcsine or arccosine operation

How to determine the data is not nan

Method a: using "a == a" to determine if it is a real number, the result returns true, nan data returns false.

#include<iostream>
#include<cmath>
int main()
{
float a = sqrt(2);
float b = sqrt(-2);
a == a ? std::cout << "real number" << std::endl;
         std::cout << "nan number" << std::endl;
b == b ? std::cout << "real number" << std::endl;
         std::cout << "nan number" << std::endl;
return 0;        
}

Option two: "isnan ()" function

#include<iostream>
#include<cmath>
int main()
{
float a = sqrt(2);
float b = sqrt(-2);
isnan(a) ? std::cout << "nan number" << std::endl;
         std::cout << "real number" << std::endl;
isnan(b) ? std::cout << "nan number" << std::endl;
         std::cout << "real number" << std::endl;
return 0;        
}

nan point

pcl::pointXYZI nan_point;
nan_point.x = NAN;
nan_point.y = NAN;
nan_point.z = NAN;
Published 10 original articles · won praise 2 · Views 753

Guess you like

Origin blog.csdn.net/woaizhiyang/article/details/104065271