From b40601c6e65b10e070dc3d025ffa50850b7d4ebb Mon Sep 17 00:00:00 2001 From: Pjotr Prins Date: Thu, 6 Sep 2018 12:59:12 +0000 Subject: Sometimes a value gets negative zero causing a NaN on the sqrt. Closes #61 --- src/mathfunc.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src/mathfunc.cpp') diff --git a/src/mathfunc.cpp b/src/mathfunc.cpp index fdd6a58..542093e 100644 --- a/src/mathfunc.cpp +++ b/src/mathfunc.cpp @@ -104,11 +104,22 @@ bool is_float(const std::string & s){ } double safe_log(const double d) { - if (!is_legacy_mode()) + if (!is_legacy_mode() && !is_check_mode()) enforce_msg(d > 0.0, (std::string("Trying to take the log of ") + std::to_string(d)).c_str()); return log(d); } +double safe_sqrt(const double d) { + double d1 = d; + if (fabs(d < 0.001)) + d1 = fabs(d); + if (!is_legacy_mode() && !is_check_mode()) + enforce_msg(d1 >= 0.0, (std::string("Trying to take the sqrt of ") + std::to_string(d)).c_str()); + if (d1 < 0.0 ) + return nan(""); + return sqrt(d1); +} + // calculate variance of a vector double VectorVar(const gsl_vector *v) { double d, m = 0.0, m2 = 0.0; -- cgit v1.2.3