简体   繁体   English

Rcpp程序中的最小值和最大值

[英]min and max in Rcpp programs

I was converting an R function into Rcpp function. 我正在将R函数转换为Rcpp函数。 It is all fine but I have difficulties in using the standard max and min function. 一切都很好,但我在使用标准的最大和最小功能方面遇到了困难。 The code below: 代码如下:

#include <math.h>
#include <RcppArmadillo.h>
#include <algorithm>
#include <iostream>

// [[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp;
using namespace arma;
using namespace std;

double f4RandomC(double x, double a, double b) {
  double out, temp;

  temp=(log( (a*(1-x)) / (a+x) )) /log(b) ;
  out= std::min(1,temp );
  return out;
}

returns me error "no matchinf function for call min(int, &double). If possible, I would lioke to use the std:: library min function 返回错误“没有matchinf函数用于调用min(int,&double)。如果可能的话,我会lioke使用std :: library min函数

Just change std::min(1,temp) to std::min(1.0,temp) : 只需将std::min(1,temp)更改为std::min(1.0,temp)

#include <cmath>
#include <Rcpp.h>
// [[Rcpp::export]]
double f4RandomC(double x, double a, double b) {
  double out, temp;

  temp =(log( (a*(1-x)) / (a+x) )) /log(b) ;
  out = std::min(1.0,temp );
  return out;
}

I'm assuming this has to do with the template definition of std::min 我假设这与std::min的模板定义有关

template <class T> const T& min (const T& a, const T& b);

which is defined in terms of only one type ( T ), whereas you were passing it two data types ( int and double ). 它只根据一种类型( T )定义,而您传递了两种数据类型( intdouble )。

Or since you are only comparing two values, you could do this a little more concisely by replacing std::min with the ternary operator ( ?: ): 或者因为你只是比较两个值,你可以通过用三元运算符( ?: :)替换std::min更简洁地做到这一点:

double f4RandomC(double x, double a, double b) {
  double temp;

  temp =(log( (a*(1-x)) / (a+x) )) /log(b) ;
  return temp < 1 ? temp : 1;
}

I'm guessing the type deduction is a little more flexible for operator< than std::min . 我猜测类型推导对于operator<std::min更灵活一点。

Two other options with std::min : std::min另外两个选项:

// [[Rcpp::export]]
double f4RandomC2(double x, double a, double b) {
  double out, temp;
  int z = 1;
  temp =(log( (a*(1-x)) / (a+x) )) /log(b) ;
  out = std::min( static_cast<double>(z),temp );
  return out;
}

// [[Rcpp::export]]
double f4RandomC3(double x, double a, double b) {
  double out, temp;
  int z = 1;
  temp =(log( (a*(1-x)) / (a+x) )) /log(b) ;
  out = std::min<double>( z,temp );
  return out;
}

Although in this case it's certainly much easier to just change 1 to 1.0 than to (unnecessarily) define int z just to cast it to a double anyways later on. 虽然在这种情况下,将1更改为1.0肯定要比(不必要地)定义int z更容易,只是为了将其转换为双倍。

You can learn a lot by reading through function / class definitions (as with most programming languages) - cplusplus.com and cppreference.com are pretty standard sources - and it will often make compiler errors seem much less cryptic. 通过阅读函数/类定义(与大多数编程语言一样),您可以学到很多东西--cplusplus.comcppreference.com是非常标准的资源 - 而且它经常会使编译器错误看起来更加神秘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM