numeric_limits 解析

本文介绍了C++标准库中的numeric_limits特性,它有助于理解不同编译环境下基本数据类型的实现细节,例如位数和取值范围等。通过使用numeric_limits,开发者能够更好地确保代码在不同平台间的可移植性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

初次碰到numeric_limits不知道这个可以用来干嘛,可以这么说,这个是用来为移植的方便性设计的。

c++移植的时候有这么一条经验,为了达到最大程度的可移植性,一种明智的做法是让我们所依赖的由实现定义的特征明确化,将更微妙的实例孤立到程序里一些清楚标明的部分之中。

一种典型的实际做法就是将所有对硬件的依赖表述为一种常量和类型定义,放到某个头文件里。正是为了支持这类技术,标准库提供了numeric_limits 。


说白了:便是 让我们程序员知道 我们现在所实现的环境是什么样的环境,char  short  int  float long double 具体是怎么实现的,它们的位数和取值范围是多少,好在进行移植的时候进行代码改写。如在32位机子中  int 型为32位,即4个字节,现在我要把在这个环境中的程序移植到16位的机子上来,通过numeric_limits 我们可以知道在16位上的int 只有16位,而long 有32位。看下面的例子:

32位机子中    typedef int int32;    int32 a;       // 要用 numeric_limits 知道此时的环境

16位机子中    typedef long int32;  int32 a;

这样进行移植的时候就不会发生错误了。


弄明白了numeric_limits是用来干嘛的,接下来我们来解释下numeric_limits 用法,网上有很多

这一篇大家可以看一下   http://blog.youkuaiyun.com/netrookie/article/details/5530578

我推荐大家还是直接看 http://www.cplusplus.com/reference/limits/numeric_limits/ 


#include <pcl/io/pcd_io.h> #include <pcl/point_types.h> #include <opencv2/opencv.hpp> #include <iostream> int main(int argc, char** argv) { // 读取点云数据 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); if (pcl::io::loadPCDFile<pcl::PointXYZ>("bunny.pcd", *cloud) == -1) { PCL_ERROR("Couldn't read the PCD file\n"); return -1; } // 初始化最值 float x_min = std::numeric_limits<float>::max(); float x_max = std::numeric_limits<float>::lowest(); float y_min = std::numeric_limits<float>::max(); float y_max = std::numeric_limits<float>::lowest(); float z_min = std::numeric_limits<float>::max(); float z_max = std::numeric_limits<float>::lowest(); // 计算边界框 for (const auto& point : cloud->points) { if (point.x < x_min) x_min = point.x; if (point.x > x_max) x_max = point.x; if (point.y < y_min) y_min = point.y; if (point.y > y_max) y_max = point.y; if (point.z < z_min) z_min = point.z; if (point.z > z_max) z_max = point.z; } // 设置分辨率 float resolution = 0.001f; // 每个像素表示0.01米 // 计算图像大小 int width = static_cast<int>((x_max - x_min) / resolution) + 1; int height = static_cast<int>((y_max - y_min) / resolution) + 1; // 创建深度图像 cv::Mat depth_image = cv::Mat::zeros(height, width, CV_32FC1); // 填充深度图像 for (const auto& point : cloud->points) { int u = static_cast<int>((point.x - x_min) / resolution); int v = static_cast<int>((point.y - y_min) / resolution); float depth_value = point.z depth_image.at<float>(v, u) = depth_value; } // 显示深度图像 cv::imshow("Depth Image", depth_image); cv::waitKey(0); return 0; }代码实现功能和原理解析,给出必要的数学公式
最新发布
06-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值