aboutsummaryrefslogtreecommitdiff
path: root/src/mathfunc.cpp
diff options
context:
space:
mode:
authorPjotr Prins2018-09-06 12:59:12 +0000
committerPjotr Prins2018-09-06 12:59:12 +0000
commitb40601c6e65b10e070dc3d025ffa50850b7d4ebb (patch)
tree9cd3f7138af5a2cb077c03edf66aa4638093b4d1 /src/mathfunc.cpp
parentcdf407bd7994dbe41a952bf29cacc1a2ca9c722e (diff)
downloadpangemma-b40601c6e65b10e070dc3d025ffa50850b7d4ebb.tar.gz
Sometimes a value gets negative zero causing a NaN on the sqrt.
Closes #61
Diffstat (limited to 'src/mathfunc.cpp')
-rw-r--r--src/mathfunc.cpp13
1 files changed, 12 insertions, 1 deletions
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;