Skip to content

Commit c2c5bde

Browse files
committed
reuse existing macro as per Ben suggestion
1 parent 4590f5b commit c2c5bde

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

src/froll.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,8 +1137,6 @@ void frollprodExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill
11371137
}
11381138
}
11391139

1140-
#define CLAMP0(x) (((x) < 0) ? 0 : (x))
1141-
11421140
/* fast rolling var - fast
11431141
Welford's online algorithm
11441142
numerically stable
@@ -1172,7 +1170,7 @@ void frollvarFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill,
11721170
wmean += delta / (i + 1);
11731171
m2 += delta * (x[i] - wmean);
11741172
double ans_i = m2 / k0;
1175-
ans->dbl_v[i] = CLAMP0(ans_i);
1173+
ans->dbl_v[i] = MAX(0,ans_i);
11761174
if (R_FINITE((double) m2)) {
11771175
for (uint64_t i=k; i<nx; i++) {
11781176
double x_out = x[i-k];
@@ -1184,7 +1182,7 @@ void frollvarFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill,
11841182
wmean += delta_in / k;
11851183
m2 += delta_in * (x_in - wmean);
11861184
double ans_i = m2 / k0;
1187-
ans->dbl_v[i] = CLAMP0(ans_i);
1185+
ans->dbl_v[i] = MAX(0,ans_i);
11881186
}
11891187
if (!R_FINITE((double) m2)) {
11901188
if (hasnf==-1)
@@ -1255,7 +1253,7 @@ void frollvarExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill,
12551253
wsumxi += (xi * xi);
12561254
}
12571255
double ans_i = wsumxi / (k - 1);
1258-
ans->dbl_v[i] = CLAMP0(ans_i);
1256+
ans->dbl_v[i] = MAX(0,ans_i);
12591257
}
12601258
}
12611259
if (truehasnf) {
@@ -1291,7 +1289,7 @@ void frollvarExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill,
12911289
wsumxi += (xi * xi);
12921290
}
12931291
double ans_i = wsumxi / (k - 1);
1294-
ans->dbl_v[i] = CLAMP0(ans_i);
1292+
ans->dbl_v[i] = MAX(0,ans_i);
12951293
} else if (nc < (k - 1)) { // var(scalar) is also NA thats why k-1 so at least 2 numbers must be there
12961294
long double wmean = wsum / (k - nc);
12971295
long double xi = 0.0;
@@ -1303,7 +1301,7 @@ void frollvarExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill,
13031301
}
13041302
}
13051303
double ans_i = wsumxi / (k - nc - 1);
1306-
ans->dbl_v[i] = CLAMP0(ans_i);
1304+
ans->dbl_v[i] = MAX(0,ans_i);
13071305
} else {
13081306
ans->dbl_v[i] = NA_REAL;
13091307
}

src/frolladaptive.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -737,8 +737,6 @@ void frolladaptiveprodExact(const double *x, uint64_t nx, ans_t *ans, const int
737737
}
738738
}
739739

740-
#define CLAMP0(x) (((x) < 0) ? 0 : (x))
741-
742740
/* fast rolling adaptive var - exact
743741
*/
744742
void frolladaptivevarExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose) {
@@ -779,7 +777,7 @@ void frolladaptivevarExact(const double *x, uint64_t nx, ans_t *ans, const int *
779777
wsumxi += (xi * xi);
780778
}
781779
double ans_i = (wsumxi / (k[i] - 1));
782-
ans->dbl_v[i] = CLAMP0(ans_i);
780+
ans->dbl_v[i] = MAX(0,ans_i);
783781
}
784782
}
785783
}

0 commit comments

Comments
 (0)