From e20221e370af4056c05b5adc844a27cb22e6c3c9 Mon Sep 17 00:00:00 2001 From: Andrew Pulsipher Date: Thu, 23 Jan 2025 15:25:02 -0700 Subject: [PATCH] Catch -inf runtime error --- epiworld.hpp | 19 +++++++++++++++++-- .../math/lfmcmc/lfmcmc-meat-print.hpp | 19 +++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/epiworld.hpp b/epiworld.hpp index 9a258db8..7c368b07 100644 --- a/epiworld.hpp +++ b/epiworld.hpp @@ -2243,7 +2243,15 @@ inline void LFMCMC::print(size_t burnin) const for (auto & n : summ_params) { - int tmp_nchar = std::floor(std::log10(std::abs(n))); + int tmp_nchar; + + if (n == 0) { + // std::log10 will return -inf and throw a runtime error + tmp_nchar = s; + } else { + tmp_nchar = std::floor(std::log10(std::abs(n))); + } + if (nchar_par_num < tmp_nchar) nchar_par_num = tmp_nchar; } @@ -2311,7 +2319,14 @@ inline void LFMCMC::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 (s == 0) { + // std::log10 will return -inf and throw a runtime error + tmp_nchar = s; + } else { + tmp_nchar = std::floor(std::log10(std::abs(s))); + } + if (nchar < tmp_nchar) nchar = tmp_nchar; } diff --git a/include/epiworld/math/lfmcmc/lfmcmc-meat-print.hpp b/include/epiworld/math/lfmcmc/lfmcmc-meat-print.hpp index 6a658ab5..c33a7b61 100755 --- a/include/epiworld/math/lfmcmc/lfmcmc-meat-print.hpp +++ b/include/epiworld/math/lfmcmc/lfmcmc-meat-print.hpp @@ -93,7 +93,15 @@ inline void LFMCMC::print(size_t burnin) const for (auto & n : summ_params) { - int tmp_nchar = std::floor(std::log10(std::abs(n))); + int tmp_nchar; + + if (n == 0) { + // std::log10 will return -inf and throw a runtime error + tmp_nchar = s; + } else { + tmp_nchar = std::floor(std::log10(std::abs(n))); + } + if (nchar_par_num < tmp_nchar) nchar_par_num = tmp_nchar; } @@ -161,7 +169,14 @@ inline void LFMCMC::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 (s == 0) { + // std::log10 will return -inf and throw a runtime error + tmp_nchar = s; + } else { + tmp_nchar = std::floor(std::log10(std::abs(s))); + } + if (nchar < tmp_nchar) nchar = tmp_nchar; }