some fix to make it more c++

This commit is contained in:
tqchen 2014-09-01 22:06:10 -07:00
parent 50f1b5d903
commit 42fb7b4d9d
7 changed files with 26 additions and 21 deletions

View File

@ -2,7 +2,7 @@ export CC = gcc
export CXX = g++
export LDFLAGS= -pthread -lm
export CFLAGS = -Wall -O3 -msse2 -Wno-unknown-pragmas -fPIC -pedantic -ansi
export CFLAGS = -Wall -O3 -msse2 -Wno-unknown-pragmas -fPIC -pedantic -std=c++98 -Wpedantic -pedantic-errors
ifeq ($(no_omp),1)
CFLAGS += -DDISABLE_OPENMP

View File

@ -8,6 +8,7 @@
#include "src/utils/utils.h"
#include "src/utils/omp.h"
#include "src/utils/matrix_csr.h"
using namespace std;
using namespace xgboost;
extern "C" {
@ -18,10 +19,12 @@ extern "C" {
// implements error handling
namespace xgboost {
namespace utils {
void (*Printf)(const char *fmt, ...) = Rprintf;
void (*Assert)(int exp, const char *fmt, ...) = XGBoostAssert_R;
void (*Check)(int exp, const char *fmt, ...) = XGBoostCheck_R;
void (*Error)(const char *fmt, ...) = error;
extern "C" {
void (*Printf)(const char *fmt, ...) = Rprintf;
void (*Assert)(int exp, const char *fmt, ...) = XGBoostAssert_R;
void (*Check)(int exp, const char *fmt, ...) = XGBoostCheck_R;
void (*Error)(const char *fmt, ...) = error;
}
} // namespace utils
namespace random {

View File

@ -8,8 +8,8 @@
#include <vector>
#include <utility>
#include <string>
#include <climits>
#include <cmath>
#include <climits>
#include <algorithm>
#include "./evaluation.h"
#include "./helper_utils.h"

View File

@ -7,6 +7,7 @@
*/
#include <utility>
#include <vector>
#include <cmath>
#include <algorithm>
namespace xgboost {
namespace learner {

View File

@ -6,9 +6,9 @@
* \author Tianqi Chen, Kailong Chen
*/
#include <vector>
#include <cmath>
#include <algorithm>
#include <utility>
#include <cmath>
#include <functional>
#include "../data.h"
#include "./objective.h"
@ -37,7 +37,7 @@ struct LossType {
case kLogisticRaw:
case kLinearSquare: return x;
case kLogisticClassify:
case kLogisticNeglik: return 1.0f / (1.0f + expf(-x));
case kLogisticNeglik: return 1.0f / (1.0f + std::exp(-x));
default: utils::Error("unknown loss_type"); return 0.0f;
}
}
@ -50,7 +50,7 @@ struct LossType {
inline float FirstOrderGradient(float predt, float label) const {
switch (loss_type) {
case kLinearSquare: return predt - label;
case kLogisticRaw: predt = 1.0f / (1.0f + expf(-predt));
case kLogisticRaw: predt = 1.0f / (1.0f + std::exp(-predt));
case kLogisticClassify:
case kLogisticNeglik: return predt - label;
default: utils::Error("unknown loss_type"); return 0.0f;
@ -65,7 +65,7 @@ struct LossType {
inline float SecondOrderGradient(float predt, float label) const {
switch (loss_type) {
case kLinearSquare: return 1.0f;
case kLogisticRaw: predt = 1.0f / (1.0f + expf(-predt));
case kLogisticRaw: predt = 1.0f / (1.0f + std::exp(-predt));
case kLogisticClassify:
case kLogisticNeglik: return predt * (1 - predt);
default: utils::Error("unknown loss_type"); return 0.0f;
@ -80,7 +80,7 @@ struct LossType {
loss_type == kLogisticNeglik ) {
utils::Check(base_score > 0.0f && base_score < 1.0f,
"base_score must be in (0,1) for logistic loss");
base_score = -logf(1.0f / base_score - 1.0f);
base_score = -std::log(1.0f / base_score - 1.0f);
}
return base_score;
}
@ -419,8 +419,8 @@ class LambdaRankObjNDCG : public LambdaRankObj {
for (size_t i = 0; i < pairs.size(); ++i) {
unsigned pos_idx = pairs[i].pos_index;
unsigned neg_idx = pairs[i].neg_index;
float pos_loginv = 1.0f / logf(pos_idx + 2.0f);
float neg_loginv = 1.0f / logf(neg_idx + 2.0f);
float pos_loginv = 1.0f / std::log(pos_idx + 2.0f);
float neg_loginv = 1.0f / std::log(neg_idx + 2.0f);
int pos_label = static_cast<int>(sorted_list[pos_idx].label);
int neg_label = static_cast<int>(sorted_list[neg_idx].label);
float original =
@ -438,7 +438,7 @@ class LambdaRankObjNDCG : public LambdaRankObj {
for (size_t i = 0; i < labels.size(); ++i) {
const unsigned rel = static_cast<unsigned>(labels[i]);
if (rel != 0) {
sumdcg += ((1 << rel) - 1) / logf(static_cast<float>(i + 2));
sumdcg += ((1 << rel) - 1) / std::log(static_cast<float>(i + 2));
}
}
return static_cast<float>(sumdcg);

View File

@ -91,7 +91,7 @@ struct Random{
// use rand instead of rand_r in windows, for MSVC it is fine since rand is threadsafe
// For cygwin and mingw, this can slows down parallelism, but rand_r is only used in objective-inl.hpp, won't affect speed in general
// todo, replace with another PRNG
#if defined(_MSC_VER)||defined(_WIN32)
#if defined(_MSC_VER)||defined(_WIN32)||defined(XGBOOST_STRICT_CXX98_)
return Uniform();
#else
return static_cast<double>(rand_r(&rseed)) / (static_cast<double>(RAND_MAX) + 1.0);

View File

@ -15,7 +15,7 @@
#endif
#if !defined(__GNUC__)
#define fopen64 fopen
#define fopen64 std::fopen
#endif
#ifdef _MSC_VER
// NOTE: sprintf_s is not equivalent to snprintf,
@ -31,7 +31,7 @@
#ifdef __APPLE__
#define off64_t off_t
#define fopen64 fopen
#define fopen64 std::fopen
#endif
extern "C" {
@ -52,6 +52,7 @@ typedef long int64_t;
namespace xgboost {
/*! \brief namespace for helper utils of the project */
namespace utils {
/*! \brief error message buffer length */
const int kPrintBuffer = 1 << 12;
@ -86,10 +87,10 @@ void HandlePrint(const char *msg);
#ifdef XGBOOST_STRICT_CXX98_
// these function pointers are to be assigned
extern void (*Printf)(const char *fmt, ...);
extern void (*Assert)(int exp, const char *fmt, ...);
extern void (*Check)(int exp, const char *fmt, ...);
extern void (*Error)(const char *fmt, ...);
extern "C" void (*Printf)(const char *fmt, ...);
extern "C" void (*Assert)(int exp, const char *fmt, ...);
extern "C" void (*Check)(int exp, const char *fmt, ...);
extern "C" void (*Error)(const char *fmt, ...);
#else
/*! \brief printf, print message to the console */
inline void Printf(const char *fmt, ...) {