Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use lgamma_r because lgamma is not thread safe #521

Merged
merged 4 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading