std::isnan
时间: 2023-08-15 18:13:57 浏览: 76
`std::isnan`是C++标准库中的一个函数,它用于检查一个值是否为NaN(Not-a-Number)。NaN是一种特殊的浮点数值,表示无效或未定义的结果。
`std::isnan`函数的声明位于头文件`<cmath>`中,并且适用于浮点数类型(float、double和long double)。它的函数原型如下:
```cpp
bool isnan(float arg);
bool isnan(double arg);
bool isnan(long double arg);
```
该函数接受一个参数,返回一个布尔值。如果参数的值为NaN,则返回true;否则,返回false。
在你提供的代码中,`std::isnan(scan_in.ranges[i])`用于检查`scan_in.ranges[i]`是否为NaN。如果是NaN,将会执行相应的逻辑操作。
相关问题
void NCut::GetPatchFeature(const MatrixXf &points, MatrixXf *features_in) { MatrixXf &features = *features_in; const int num_points = static_cast<int>(points.rows()); const int dim = _patch_size * _patch_size; features.resize(num_points, dim); for (int i = 0; i < num_points; ++i) { // .1 patch int irow = 0; int jcol = 0; _ff_feature_grid.Pos2d(points.coeffRef(i, 0), points.coeffRef(i, 1), &irow, &jcol); cv::Mat patch; cv::Point2f pt(static_cast<float>(irow), static_cast<float>(jcol)); cv::getRectSubPix(_cv_feature_map, cv::Size(_patch_size, _patch_size), pt, patch); // .2 maybe later i will add other features based on patch // .3 add to features assert(dim == patch.rows * patch.cols); int p = 0; for (int r = 0; r < patch.rows; ++r) { for (int c = 0; c < patch.cols; ++c) { float val = patch.at<float>(r, c); features.coeffRef(i, p++) = static_cast<float>( (std::isnan(val) || std::isinf(val)) ? 1.e-50 : val); // features.coeffRef(i, p++) = patch.at<float>(r, c); } } } }
这段代码是用于计算NCut算法中的图像块的特征向量。给定一个点集points,对于每个点,首先通过_ff_feature_grid将其映射到_feature_map图像上,并取该点为中心的_patch_size * _patch_size大小的矩形区域作为图像块。然后,对于每个图像块,将像素值添加到特征向量中,特征向量的维度为_patch_size * _patch_size。在添加每个像素值时,如果该值为NaN或Inf,则设置为1.e-50。最终,返回一个特征向量矩阵features,其行数为points中点的个数,列数为_patch_size * _patch_size。
c++ isnan
C++ 中的 isnan 函数用于判断一个浮点数是否为 NaN(Not a Number)。如果是 NaN,函数返回 true;否则,返回 false。该函数定义在头文件 cmath 中。以下是一个示例:
```c++
#include <iostream>
#include <cmath>
int main() {
double x = std::sqrt(-1.0);
if (std::isnan(x)) {
std::cout << "x is NaN.\n";
} else {
std::cout << "x is not NaN.\n";
}
return 0;
}
```
输出:
```
x is NaN.
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)