boost 中round和roundf函数的用法

本文介绍了解决C++中round()和roundf()函数编译错误的方法,包括添加boost库并配置VS2012,以及如何正确地在代码中使用这些函数。

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

round()和roundf()函数是我在hog3D源码中碰到的,编译的过程中总是报错,通过网上查找发现round()函数和roundf()函数都是C++ boost库中的函数,如果需要调用这两个函数,需要:

注:我是在vs2012的环境下进行编译的,所以需要先下载boost库,然后对vs2012做相应的配置,具体步骤可参考:http://blog.youkuaiyun.com/chenkent888/article/details/10229369

(1)添加引用头文件:

#include <boost/math/special_functions/round.hpp>   //对应于round()函数的头文件

#include <boost/math/tr1.hpp>   //对应于roundf()函数的头文件

(2)由于在 boost::math::tr1命名空间中支持C99中的函数

double round(double x);
float roundf(float x);
long double roundl(long double x);

在 boost::math命名空间中,定义的round()函数为:

template <class T, class Policy>
inline T round(const T& v, const Policy& pol)
{
   BOOST_MATH_STD_USING
   if(!(boost::math::isfinite)(v))
      return policies::raise_rounding_error("boost::math::round<%1%>(%1%)", 0, v, v, pol);
   return v < 0 ? static_cast<T>(ceil(v - 0.5f)) : static_cast<T>(floor(v + 0.5f));
}
template <class T>
inline T round(const T& v)
{
   return round(v, policies::policy<>());
}
所以,在函数中使用round()函数和roundf()函数时需要加上命名空间,使用

boost::math::round(),这时候调用的即为上面的模板函数

boost::math::tr1::roundf(),调用的就为c99中的 float roundf(float x)函数。

注:添加命名空间的好处不仅能够区分不同命名空间定义的相同函数,而且对于开发的大规模的软件,它的编译时间是很长的,boost库已经在系统中安装创建,然后添加#include <boost/math/tr1.hpp> 并且使用 boost::math::tr1::roundf(x) 将会加速编译时间,减小目标文件的大小,而且还可以加快调试运行时间。



import pandas as pd import numpy as np import lightgbm as lgb from sklearn.metrics import mean_squared_error # 1. 读取合并好的数据 df = pd.read_csv('P001-2.csv', parse_dates=['time'], dtype={'annotation': str}) # 2. 特征工程(生成时间特征) def create_time_features(df): df['hour'] = df['time'].dt.hour df['weekday'] = df['time'].dt.weekday df['hour_sin'] = np.sin(2 * np.pi * df['hour'] / 24) df['hour_cos'] = np.cos(2 * np.pi * df['hour'] / 24) df['weekday_sin'] = np.sin(2 * np.pi * df['weekday'] / 7) df['weekday_cos'] = np.cos(2 * np.pi * df['weekday'] / 7) return df df = create_time_features(df) # 3. 处理 annotation 列 df['annotation'] = pd.to_numeric(df['annotation'], errors='coerce') # 4. 拆分有标签的训练集 & 缺失的测试集 train_df = df.dropna(subset=['annotation']) test_df = df[df['annotation'].isnull()] # 特征选择 features = ['x', 'y', 'z', 'hour_sin', 'hour_cos', 'weekday_sin', 'weekday_cos'] # 5. 时间排序 + 划分训练/验证 train_df = train_df.sort_values('time') split_index = int(len(train_df) * 0.8) X_train = train_df[features].iloc[:split_index] y_train = train_df['annotation'].iloc[:split_index] X_valid = train_df[features].iloc[split_index:] y_valid = train_df['annotation'].iloc[split_index:] # 6. 训练 LightGBM 回归模型 params = { 'objective': 'regression', 'metric': 'l2', 'boosting_type': 'gbdt', 'num_leaves': 63, 'learning_rate': 0.02, 'feature_fraction': 0.8, 'bagging_fraction': 0.8, 'num_threads': -1, 'verbosity': -1 } train_data = lgb.Dataset(X_train, label=y_train) valid_data = lgb.Dataset(X_valid, label=y_valid, reference=train_data) model = lgb.train( params, train_data, valid_sets=[valid_data], num_boost_round=1000, early_stopping_rounds=50 ) # 7. 填补 annotation 缺失值 X_test = create_time_features(test_df)[features] test_df['annotation'] = model.predict(X_test, num_iteration=model.best_iteration) # 8. 合并训练集 & 填补的测试集 final_df = pd.concat([train_df, test_df], ignore_index=True).sort_values('time') # 9. 评估 y_pred = model.predict(X_valid, num_iteration=model.best_iteration) mse = mean_squared_error(y_valid, y_pred) print(f"最终模型 MSE: {mse:.4f}") # 10. 保存完整填补后的数据 final_df.to_csv('P001-2_filled.csv', index=False) print(final_df[['time', 'annotation']].head()) -------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[55], line 55 52 train_data = lgb.Dataset(X_train, label=y_train) 53 valid_data = lgb.Dataset(X_valid, label=y_valid, reference=train_data) ---> 55 model = lgb.train( 56 params, 57 train_data, 58 valid_sets=[valid_data], 59 num_boost_round=1000, 60 early_stopping_rounds=50 61 ) 63 # 7. 填补 annotation 缺失值 64 X_test = create_time_features(test_df)[features] TypeError: train() got an unexpected keyword argument 'early_stopping_rounds'、
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值