From 507130bd4c8f1e89552f90f91768b056191ef565 Mon Sep 17 00:00:00 2001 From: chinandrew Date: Tue, 12 Sep 2023 22:18:28 -0400 Subject: [PATCH 1/2] Return NA if either sd in the denominator is 0. --- src/sliding_cor_c.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/sliding_cor_c.cpp b/src/sliding_cor_c.cpp index 678dd33..0c002b2 100644 --- a/src/sliding_cor_c.cpp +++ b/src/sliding_cor_c.cpp @@ -9,6 +9,10 @@ NumericVector sliding_cor_c(NumericVector shortvec, NumericVector longvec, doubl int n = shortvec.size(); int n_minus1 = n - 1; int out_length = length_longvec - n_minus1; + if (sd_shortvec < sqrt(DBL_EPSILON)) { + NumericVector out(out_length, NA_REAL); + return(out); + } NumericVector out(out_length); // Calculate constant term to multiply all @@ -37,7 +41,11 @@ NumericVector sliding_cor_c(NumericVector shortvec, NumericVector longvec, doubl ss_longvec_current += pow(longvec_current_b - mean_longvec_current, 2); } double sd_longvec_current = sqrt(ss_longvec_current / n_minus1); - out[a] = (sum_products / n_minus1 - sum_longvec_current * term2) / sd_shortvec / sd_longvec_current; + if (sd_longvec_current < sqrt(DBL_EPSILON)) { + out[a] = NA_REAL; + } else { + out[a] = (sum_products / n_minus1 - sum_longvec_current * term2) / sd_shortvec / sd_longvec_current; + } } return out; -} \ No newline at end of file +} From 9e4de881fe6c266f2d541bdade5fe1e22f5479e9 Mon Sep 17 00:00:00 2001 From: chinandrew Date: Wed, 13 Sep 2023 10:07:17 -0400 Subject: [PATCH 2/2] Align return statement format with previous code --- src/sliding_cor_c.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sliding_cor_c.cpp b/src/sliding_cor_c.cpp index 0c002b2..73f575b 100644 --- a/src/sliding_cor_c.cpp +++ b/src/sliding_cor_c.cpp @@ -11,7 +11,7 @@ NumericVector sliding_cor_c(NumericVector shortvec, NumericVector longvec, doubl int out_length = length_longvec - n_minus1; if (sd_shortvec < sqrt(DBL_EPSILON)) { NumericVector out(out_length, NA_REAL); - return(out); + return out; } NumericVector out(out_length);