Ruby浮点数比较警告:安全使用inexact_equality_warning宝石

需积分: 9 0 下载量 24 浏览量 更新于2025-01-21 收藏 7KB ZIP 举报
标题中提到的“ruby-inexact_equality_warning: 您不想比较浮点数是否相等”所涉及的知识点是关于在Ruby编程语言中处理浮点数比较的问题。在许多编程语言中,包括Ruby,浮点数的存储方式可能导致它们不能精确地表示某些实数,这是由于浮点数通常遵循IEEE 754标准,该标准使用二进制格式来近似地表示十进制数,因此无法精确地表示所有的小数。 在Ruby中,直接使用等号“==”来比较两个浮点数可能会导致意外的结果,因为浮点数的表示误差可能会导致两个在数学意义上相等的数在计算机中的二进制表示略有不同。例如,在给定的描述中,变量x被赋值为1.0 - 0.9 - 0.1,数学上等于0,但在实际的浮点数运算中得到的结果是一个非常接近0的极小数值(-2.7755575615628914e-17)。当尝试用x == 0.0来比较时,结果是false,这是因为0.0和x的二进制表示不完全相同,尽管它们在实际应用中可以认为是等效的。 因此,当涉及到浮点数相等性的判断时,建议使用一些误差范围内的比较方法而不是直接使用“==”。在Ruby社区中,开发了一个名为"inexact_equality_warning"的gem(Ruby的一种打包和分发代码的格式),用于向开发者警告在进行不精确浮点数比较时的潜在问题。 在描述中提到的安装使用信息,指导用户如何在Ruby项目中安装并使用"inexact_equality_warning" gem。用户首先需要在他们的Gemfile中添加gem "inexact_equality_warning",然后执行bundle install命令来安装该gem。之后,通过require "inexact_equality_warning"命令来引入这个库,使得它提供的警告功能生效。 最后,关于给出的文件信息中的【压缩包子文件的文件名称列表】中提到了"ruby-inexact_equality_warning-master"。这表明存在一个名为"ruby-inexact_equality_warning"的压缩包文件,其中的-master通常表示这是项目的主分支或主版本。该文件可能包含有关"inexact_equality_warning" gem的源代码、文档、测试以及其它开发资源,用于项目开发和维护。 针对这类问题,程序员在编程实践中应当注意以下几点: 1. 避免直接比较浮点数。而是应该设定一个合理的误差范围(例如一个很小的浮点数epsilon),然后判断两个浮点数的差的绝对值是否小于这个epsilon。 2. 使用数学库提供的浮点数比较函数。一些数学库提供了用于比较浮点数是否在一定误差范围内的函数,这些函数可以自动处理比较的精度问题。 3. 使用Ruby的Rational类或BigDecimal类。这两个类分别用于处理精确的有理数和高精度的小数计算。当涉及到需要高精度的数学计算时,它们会是更好的选择。 4. 在使用"inexact_equality_warning" gem时,要根据gem提供的警告信息调整代码中的浮点数比较逻辑,以避免因为浮点数精度问题带来的bug。

void process_file(const std::string& input_file, const std::string& output_file) { // ----------------- 1. 读取LAZ文件 ----------------- pdal::Option las_opt("filename", input_file); pdal::Options las_opts; las_opts.add(las_opt); pdal::LasReader reader; reader.setOptions(las_opts); pdal::PointTable table; reader.prepare(table); pdal::PointViewSet viewSet = reader.execute(table); pdal::PointViewPtr view = *viewSet.begin(); // ----------------- 2. 提取地面点(分类码2) ----------------- std::vector<std::tuple<double, double, double>> ground_points; // 存储XYZ for (pdal::PointId i = 0; i < view->size(); ++i) { if (view->getFieldAs<int>(pdal::Dimension::Id::Classification, i) == 2) { double x = view->getFieldAs<double>(pdal::Dimension::Id::X, i); double y = view->getFieldAs<double>(pdal::Dimension::Id::Y, i); double z = view->getFieldAs<double>(pdal::Dimension::Id::Z, i); ground_points.emplace_back(x, y, z); } } if (ground_points.empty()) { std::cerr << "警告: " << input_file << " 中没有地面点!" << std::endl; return; } // ----------------- 3. 构建Delaunay三角网 ----------------- std::vector<Point_2> points; for (const auto& pt : ground_points) { points.emplace_back(std::get<0>(pt), std::get<1>(pt)); } Delaunay dt(points.begin(), points.end()); // ----------------- 4. 计算数据范围 ----------------- double min_x = 1e9, max_x = -1e9; double min_y = 1e9, max_y = -1e9; for (const auto& pt : ground_points) { min_x = std::min(min_x, std::get<0>(pt)); max_x = std::max(max_x, std::get<0>(pt)); min_y = std::min(min_y, std::get<1>(pt)); max_y = std::max(max_y, std::get<1>(pt)); } 解释每一行代码

2025-03-10 上传

int main(int argc, const char** argv) { //****************************************获取数据***************************************************** const std::string input_filename = (argc > 1) ? argv[1] : CGAL::data_file_path("C:\\Users\\lwc\\source\\repos\\Project4\\x64\\Release\\output.xyz"); const char* output_filename = (argc > 2) ? argv[2] : "C:\\Users\\lwc\\source\\repos\\Project4\\x64\\Release\\113.xyz"; //输出文件名称 std::vector<PointVectorPair> points; if (!CGAL::IO::read_points(input_filename, std::back_inserter(points), CGAL::parameters::point_map(CGAL::First_of_pair_property_map<PointVectorPair>()) .normal_map(CGAL::Second_of_pair_property_map<PointVectorPair>()))) { std::cerr << "Error: cannot read file " << input_filename << std::endl; return EXIT_FAILURE; } //****************************************点云平滑************************************************* unsigned int k = 5; //邻近点数 double offset_radius = 0.01; CGAL::vcm_estimate_normals<std::vector<PointVectorPair>>(points, offset_radius, k, CGAL::parameters::point_map(CGAL::First_of_pair_property_map<PointVectorPair>()) .normal_map(CGAL::Second_of_pair_property_map<PointVectorPair>())); //使用vcm算法来获取每个点的法向量,后面的参数指定了我们的点与法向量对于的部分 //********************************************保存数据************************************************* if (!CGAL::IO::write_points(output_filename, points, CGAL::parameters::point_map(CGAL::First_of_pair_property_map<PointVectorPair>()) .normal_map(CGAL::Second_of_pair_property_map<PointVectorPair>()) .stream_precision(17))) return EXIT_FAILURE; std::cout << "计算结束!" << std::endl; return EXIT_SUCCESS; } 我想将此算法改成遍历文件夹应该怎么写

137 浏览量
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部