Skip to content

Commit

Permalink
Merge pull request #521 from imbs-hl/issue518
Browse files Browse the repository at this point in the history
Use lgamma_r because lgamma is not thread safe
  • Loading branch information
mnwright authored May 16, 2024
2 parents e3047c6 + 9554d01 commit 5df7565
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ Suggests:
testthat
Encoding: UTF-8
RoxygenNote: 7.3.1
URL: http://imbs-hl.github.io/ranger/,
URL: https://imbs-hl.github.io/ranger/,
https://github.com/imbs-hl/ranger
BugReports: https://github.com/imbs-hl/ranger/issues
2 changes: 1 addition & 1 deletion cpp_version/src/version.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#ifndef RANGER_VERSION
#define RANGER_VERSION "0.16.0"
#define RANGER_VERSION "0.16.1"
#endif
16 changes: 14 additions & 2 deletions src/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,20 @@ double betaLogLik(double y, double mean, double phi) {
phi = 1 - std::numeric_limits<double>::epsilon();
}

return (lgamma(phi) - lgamma(mean * phi) - lgamma((1 - mean) * phi) + (mean * phi - 1) * log(y)
+ ((1 - mean) * phi - 1) * log(1 - y));
return (mylgamma(phi) - mylgamma(mean * phi) - mylgamma((1 - mean) * phi) + (mean * phi - 1) * log(y)
+ ((1 - mean) * phi - 1) * log(1 - y));
}

double mylgamma(double x) {
#ifdef WIN_R_BUILD
// lgamma_r not available in mingw
return(lgamma(x));
#else
int gamma_sign; // Sign for gamma function, not used
using namespace std; // Because lgamma_r is sometimes in global namespace, sometimes in std
return(lgamma_r(x, &gamma_sign));
#endif

}

} // namespace ranger
6 changes: 6 additions & 0 deletions src/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,12 @@ std::stringstream& readFromStream(std::stringstream& in, double& token);
*/
double betaLogLik(double y, double mean, double phi);

/*
* Returns the natural logarithm of the absolute value of the gamma function of x.
* @param x Parameter for the log-gamma function.
*/
double mylgamma(double x);

// User interrupt from R
#ifdef R_BUILD
static void chkIntFn(void *dummy) {
Expand Down

0 comments on commit 5df7565

Please sign in to comment.