Rcpp 意外的 NaN 结果
Rcpp Unexpected NaN results
我有一个包含很多 pow 的复杂方程,得到的结果是 NaN。所以我把它分成几块,叫做temp1到temp4。 temp4
即 pow(temp3, 0.25)
是我得到一个 nan 然后返回 NaN 的地方。
#include <Rcpp.h>
#include <cmath>
using namespace Rcpp;
const double kVal = 273.15;
// [[Rcpp::export]]
double thermalRadiance(double tas, double wind, double Tg) {
double temp1 = pow(Tg + kVal, 4.0);
double temp2 = pow(wind, 0.6) * (Tg - tas);
double temp3 = (temp1 + 2.5e+8 * temp2);
double temp4 = pow(temp3, 0.25);
double tr = temp4 - kVal;
Rcpp::Rcout << "tas " << tas << " wind " << wind << " Tg " << Tg << " temp1 " << temp1 << " temp2 " << temp2 << " temp3 " << temp3 << " temp4 " << temp4 << " tr " << tr << std::endl;
return tr;
}
/*** R
thermalRadiance(29., 4.5, 17.4)
# test of temp4 in R
-2.37018e+07^.25
*/
tas 29 wind 4.5 Tg 17.4 temp1 7.12662e+09 temp2 -28.6013 temp3 -2.37018e+07 temp4 nan tr nan
[1] NaN
> # test of temp4 in R
> -2.37018e+07^.25
[1] -69.77427
在我得到 temp4 值之前看起来真的很简单。非常感谢任何帮助!
您正试图在 C++ 中取负数的四次方根。也许您应该将 pow(temp3, 0.25)
替换为 -pow(abs(temp3), 0.25)
#include <Rcpp.h>
#include <cmath>
using namespace Rcpp;
const double kVal = 273.15;
// [[Rcpp::export]]
double thermalRadiance(double tas, double wind, double Tg) {
double temp1 = pow(Tg + kVal, 4.0);
double temp2 = pow(wind, 0.6) * (Tg - tas);
double temp3 = (temp1 + 2.5e+8 * temp2);
double temp4 = -pow(abs(temp3), 0.25);
double tr = temp4 - kVal;
Rcpp::Rcout << "tas " << tas << " wind " << wind << " Tg " << Tg << " temp1 "
<< temp1 << " temp2 " << temp2 << " temp3 " << temp3 << " temp4 "
<< temp4 << " tr " << tr << std::endl;
return tr;
}
R 中的哪个给你:
thermalRadiance(29., 4.5, 17.4)
tas 29 wind 4.5 Tg 17.4 temp1 7.12662e+09 temp2 -28.6013 temp3 -2.37018e+07 temp4 -69.7743 tr -342.924
[1] -342.9243
我有一个包含很多 pow 的复杂方程,得到的结果是 NaN。所以我把它分成几块,叫做temp1到temp4。 temp4
即 pow(temp3, 0.25)
是我得到一个 nan 然后返回 NaN 的地方。
#include <Rcpp.h>
#include <cmath>
using namespace Rcpp;
const double kVal = 273.15;
// [[Rcpp::export]]
double thermalRadiance(double tas, double wind, double Tg) {
double temp1 = pow(Tg + kVal, 4.0);
double temp2 = pow(wind, 0.6) * (Tg - tas);
double temp3 = (temp1 + 2.5e+8 * temp2);
double temp4 = pow(temp3, 0.25);
double tr = temp4 - kVal;
Rcpp::Rcout << "tas " << tas << " wind " << wind << " Tg " << Tg << " temp1 " << temp1 << " temp2 " << temp2 << " temp3 " << temp3 << " temp4 " << temp4 << " tr " << tr << std::endl;
return tr;
}
/*** R
thermalRadiance(29., 4.5, 17.4)
# test of temp4 in R
-2.37018e+07^.25
*/
tas 29 wind 4.5 Tg 17.4 temp1 7.12662e+09 temp2 -28.6013 temp3 -2.37018e+07 temp4 nan tr nan
[1] NaN
> # test of temp4 in R
> -2.37018e+07^.25
[1] -69.77427
在我得到 temp4 值之前看起来真的很简单。非常感谢任何帮助!
您正试图在 C++ 中取负数的四次方根。也许您应该将 pow(temp3, 0.25)
替换为 -pow(abs(temp3), 0.25)
#include <Rcpp.h>
#include <cmath>
using namespace Rcpp;
const double kVal = 273.15;
// [[Rcpp::export]]
double thermalRadiance(double tas, double wind, double Tg) {
double temp1 = pow(Tg + kVal, 4.0);
double temp2 = pow(wind, 0.6) * (Tg - tas);
double temp3 = (temp1 + 2.5e+8 * temp2);
double temp4 = -pow(abs(temp3), 0.25);
double tr = temp4 - kVal;
Rcpp::Rcout << "tas " << tas << " wind " << wind << " Tg " << Tg << " temp1 "
<< temp1 << " temp2 " << temp2 << " temp3 " << temp3 << " temp4 "
<< temp4 << " tr " << tr << std::endl;
return tr;
}
R 中的哪个给你:
thermalRadiance(29., 4.5, 17.4)
tas 29 wind 4.5 Tg 17.4 temp1 7.12662e+09 temp2 -28.6013 temp3 -2.37018e+07 temp4 -69.7743 tr -342.924
[1] -342.9243