Skip to content

Commit

Permalink
Fix clang compiler problems from epiworldR (#50)
Browse files Browse the repository at this point in the history
* Catch -inf runtime error

* Fix typo

* Update catch to include n < 1

* Add test to lfmcmc.cpp for summary stats less than 1

* Use absolute value in check

* Comment new test case

* Bump version number
  • Loading branch information
apulsipher authored Jan 28, 2025
1 parent a9738a3 commit 2295a2a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
23 changes: 20 additions & 3 deletions epiworld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/* Versioning */
#define EPIWORLD_VERSION_MAJOR 0
#define EPIWORLD_VERSION_MINOR 6
#define EPIWORLD_VERSION_PATCH 0
#define EPIWORLD_VERSION_PATCH 1

static const int epiworld_version_major = EPIWORLD_VERSION_MAJOR;
static const int epiworld_version_minor = EPIWORLD_VERSION_MINOR;
Expand Down Expand Up @@ -2243,7 +2243,16 @@ inline void LFMCMC<TData>::print(size_t burnin) const
for (auto & n : summ_params)
{

int tmp_nchar = std::floor(std::log10(std::abs(n)));
int tmp_nchar;

if (std::abs(n) < 1) {
// std::log10(<1) will return negative number
// std::log10(0) will return -inf and throw a runtime error
tmp_nchar = 0;
} else {
tmp_nchar = std::floor(std::log10(std::abs(n)));
}

if (nchar_par_num < tmp_nchar)
nchar_par_num = tmp_nchar;
}
Expand Down Expand Up @@ -2311,7 +2320,15 @@ inline void LFMCMC<TData>::print(size_t burnin) const
int nchar = 0;
for (auto & s : summ_stats)
{
int tmp_nchar = std::floor(std::log10(std::abs(s)));
int tmp_nchar;
if (std::abs(s) < 1) {
// std::log10(<1) will return negative number
// std::log10(0) will return -inf and throw a runtime error
tmp_nchar = 0;
} else {
tmp_nchar = std::floor(std::log10(std::abs(s)));
}

if (nchar < tmp_nchar)
nchar = tmp_nchar;
}
Expand Down
2 changes: 1 addition & 1 deletion include/epiworld/epiworld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/* Versioning */
#define EPIWORLD_VERSION_MAJOR 0
#define EPIWORLD_VERSION_MINOR 6
#define EPIWORLD_VERSION_PATCH 0
#define EPIWORLD_VERSION_PATCH 1

static const int epiworld_version_major = EPIWORLD_VERSION_MAJOR;
static const int epiworld_version_minor = EPIWORLD_VERSION_MINOR;
Expand Down
21 changes: 19 additions & 2 deletions include/epiworld/math/lfmcmc/lfmcmc-meat-print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,16 @@ inline void LFMCMC<TData>::print(size_t burnin) const
for (auto & n : summ_params)
{

int tmp_nchar = std::floor(std::log10(std::abs(n)));
int tmp_nchar;

if (std::abs(n) < 1) {
// std::log10(<1) will return negative number
// std::log10(0) will return -inf and throw a runtime error
tmp_nchar = 0;
} else {
tmp_nchar = std::floor(std::log10(std::abs(n)));
}

if (nchar_par_num < tmp_nchar)
nchar_par_num = tmp_nchar;
}
Expand Down Expand Up @@ -161,7 +170,15 @@ inline void LFMCMC<TData>::print(size_t burnin) const
int nchar = 0;
for (auto & s : summ_stats)
{
int tmp_nchar = std::floor(std::log10(std::abs(s)));
int tmp_nchar;
if (std::abs(s) < 1) {
// std::log10(<1) will return negative number
// std::log10(0) will return -inf and throw a runtime error
tmp_nchar = 0;
} else {
tmp_nchar = std::floor(std::log10(std::abs(s)));
}

if (nchar < tmp_nchar)
nchar = tmp_nchar;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/00-lfmcmc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,29 @@ EPIWORLD_TEST_CASE("LFMCMC", "[Basic example]") {
REQUIRE_NOTHROW(model.print(50000));
#endif

// Add test of summary stats between [0, 1]
std::normal_distribution<epiworld_double> rnorm2(0.5, 0.5);

vec_double obsdata2;
for (size_t i = 0u; i < 50000; ++i)
obsdata2.push_back(rnorm2(*rand));

LFMCMC< vec_double > model2(obsdata2);
model2.set_rand_engine(rand);

model2.set_simulation_fun(simfun);
model2.set_summary_fun(summary_fun);
model2.set_proposal_fun(make_proposal_norm_reflective<vec_double>(.05, .0000001, 10));
model2.set_kernel_fun(kernel_fun_gaussian<vec_double>);

model2.run({1,1}, 100000, .125);
model2.print();

#ifdef CATCH_CONFIG_MAIN
REQUIRE_NOTHROW(model2.print());
#endif


#ifndef CATCH_CONFIG_MAIN
return 0;
#endif
Expand Down

0 comments on commit 2295a2a

Please sign in to comment.