Skip to content

Commit 99579c7

Browse files
committed
replace R_FINITE with c99's isfinite
1 parent e2e0173 commit 99579c7

File tree

8 files changed

+37
-36
lines changed

8 files changed

+37
-36
lines changed

src/assign.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -967,14 +967,14 @@ const char *memrecycle(const SEXP target, const SEXP where, const int start, con
967967
case INTSXP: CHECK_RANGE(int, INTEGER, val<0 || val>255, val, _("%d (type '%s') at RHS position %d taken as 0 when assigning to type '%s' %s"))
968968
case REALSXP: if (sourceIsI64)
969969
CHECK_RANGE(int64_t, REAL, val<0 || val>255, val, _("%"PRId64" (type '%s') at RHS position %d taken as 0 when assigning to type '%s' %s"))
970-
else CHECK_RANGE(double, REAL, !R_FINITE(val) || val<0.0 || val>256.0 || (int)val!=val, val, _("%f (type '%s') at RHS position %d either truncated (precision lost) or taken as 0 when assigning to type '%s' %s"))
970+
else CHECK_RANGE(double, REAL, !isfinite(val) || val<0.0 || val>256.0 || (int)val!=val, val, _("%f (type '%s') at RHS position %d either truncated (precision lost) or taken as 0 when assigning to type '%s' %s"))
971971
} break;
972972
case INTSXP:
973973
switch (TYPEOF(source)) {
974974
case REALSXP: if (sourceIsI64)
975975
CHECK_RANGE(int64_t, REAL, val!=NA_INTEGER64 && (val<=NA_INTEGER || val>INT_MAX), val, _("%"PRId64" (type '%s') at RHS position %d out-of-range (NA) when assigning to type '%s' %s"))
976976
else CHECK_RANGE(double, REAL, !ISNAN(val) && (!within_int32_repres(val) || (int)val!=val), val, _("%f (type '%s') at RHS position %d out-of-range(NA) or truncated (precision lost) when assigning to type '%s' %s"))
977-
case CPLXSXP: CHECK_RANGE(Rcomplex, COMPLEX, !((ISNAN(val.i) || (R_FINITE(val.i) && val.i==0.0)) &&
977+
case CPLXSXP: CHECK_RANGE(Rcomplex, COMPLEX, !((ISNAN(val.i) || (isfinite(val.i) && val.i==0.0)) &&
978978
(ISNAN(val.r) || (within_int32_repres(val.r) && (int)val.r==val.r))), val.r, _("%f (type '%s') at RHS position %d either imaginary part discarded or real part truncated (precision lost) when assigning to type '%s' %s"))
979979
} break;
980980
case REALSXP:
@@ -983,9 +983,9 @@ const char *memrecycle(const SEXP target, const SEXP where, const int start, con
983983
CHECK_RANGE(double, REAL, !ISNAN(val) && (!within_int64_repres(val) || (int64_t)val!=val), val, _("%f (type '%s') at RHS position %d out-of-range(NA) or truncated (precision lost) when assigning to type '%s' %s"))
984984
break;
985985
case CPLXSXP: if (targetIsI64)
986-
CHECK_RANGE(Rcomplex, COMPLEX, !((ISNAN(val.i) || (R_FINITE(val.i) && val.i==0.0)) &&
987-
(ISNAN(val.r) || (R_FINITE(val.r) && (int64_t)val.r==val.r))), val.r, _("%f (type '%s') at RHS position %d either imaginary part discarded or real part truncated (precision lost) when assigning to type '%s' %s"))
988-
else CHECK_RANGE(Rcomplex, COMPLEX, !(ISNAN(val.i) || (R_FINITE(val.i) && val.i==0.0)), val.r, _("%f (type '%s') at RHS position %d imaginary part discarded when assigning to type '%s' %s"))
986+
CHECK_RANGE(Rcomplex, COMPLEX, !((ISNAN(val.i) || (isfinite(val.i) && val.i==0.0)) &&
987+
(ISNAN(val.r) || (isfinite(val.r) && (int64_t)val.r==val.r))), val.r, _("%f (type '%s') at RHS position %d either imaginary part discarded or real part truncated (precision lost) when assigning to type '%s' %s"))
988+
else CHECK_RANGE(Rcomplex, COMPLEX, !(ISNAN(val.i) || (isfinite(val.i) && val.i==0.0)), val.r, _("%f (type '%s') at RHS position %d imaginary part discarded when assigning to type '%s' %s"))
989989
}
990990
}
991991
}

src/data.table.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <stdint.h> // for uint64_t rather than unsigned long long
2222
#include <stdarg.h> // for va_list, va_start
2323
#include <stdbool.h>
24+
#include <math.h> // isfinite
2425
#include "types.h"
2526
#include "po.h"
2627
#ifdef WIN32 // positional specifiers (%n$) used in translations; #4402

src/fastmean.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ SEXP fastmean(SEXP args)
7070
break;
7171
}
7272
s /= n;
73-
if(R_FINITE((double)s)) {
73+
if(isfinite((double)s)) {
7474
for (int i=0; i<l; ++i) {
7575
if(ISNAN(REAL(x)[i])) continue;
7676
t += (REAL(x)[i] - s);
@@ -98,7 +98,7 @@ SEXP fastmean(SEXP args)
9898
s += REAL(x)[i];
9999
}
100100
s /= l;
101-
if(R_FINITE((double)s)) {
101+
if(isfinite((double)s)) {
102102
for (int i=0; i<l; ++i) {
103103
// no NA if got this far
104104
t += (REAL(x)[i] - s);
@@ -123,7 +123,7 @@ SEXP fastmean(SEXP args)
123123
si += COMPLEX(x)[i].i;
124124
}
125125
s /= n; si /= n;
126-
if( R_FINITE((double)s) && R_FINITE((double)si) ) {
126+
if( isfinite((double)s) && isfinite((double)si) ) {
127127
for (int i=0; i<n; ++i) {
128128
t += COMPLEX(x)[i].r - s;
129129
ti += COMPLEX(x)[i].i - si;

src/forder.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,10 @@ static void range_d(double *x, int n, uint64_t *out_min, uint64_t *out_max, int
214214
uint64_t min=0, max=0;
215215
int na_count=0, infnan_count=0;
216216
int i=0;
217-
while(i<n && !R_FINITE(x[i])) { ISNA(x[i++]) ? na_count++ : infnan_count++; }
217+
while(i<n && !isfinite(x[i])) { ISNA(x[i++]) ? na_count++ : infnan_count++; }
218218
if (i<n) { max = min = dtwiddle(x[i++]); }
219219
for(; i<n; i++) {
220-
if (!R_FINITE(x[i])) { ISNA(x[i]) ? na_count++ : infnan_count++; continue; }
220+
if (!isfinite(x[i])) { ISNA(x[i]) ? na_count++ : infnan_count++; continue; }
221221
uint64_t tmp = dtwiddle(x[i]);
222222
if (tmp>max) max=tmp;
223223
else if (tmp<min) min=tmp;
@@ -426,7 +426,7 @@ uint64_t dtwiddle(double x) //const void *p, int i)
426426
uint64_t u64;
427427
} u; // local for thread safety
428428
u.d = x; //((double *)p)[i];
429-
if (R_FINITE(u.d)) {
429+
if (isfinite(u.d)) {
430430
if (u.d==0) u.d=0; // changes -0.0 to 0.0, issue #743
431431
u.u64 ^= (u.u64 & 0x8000000000000000) ? 0xffffffffffffffff : 0x8000000000000000; // always flip sign bit and if negative (sign bit was set) flip other bits too
432432
u.u64 += (u.u64 & dmask) << 1/*is this shift really correct. No need to shift*/ ; // when dround==1|2, if 8th|16th bit is set, round up before chopping last 1|2 bytes
@@ -750,15 +750,15 @@ SEXP forder(SEXP DT, SEXP by, SEXP retGrpArg, SEXP retStatsArg, SEXP sortGroupsA
750750
#pragma omp parallel for num_threads(getDTthreads(nrow, true))
751751
for (int i=0; i<nrow; i++) {
752752
uint64_t elem=0;
753-
if (!R_FINITE(xd[i])) {
753+
if (!isfinite(xd[i])) {
754754
if (isinf(xd[i])) elem = signbit(xd[i]) ? min-1 : max+1;
755755
else {
756756
if (nalast==-1) anso[i]=0; // for both NA and NaN
757757
elem = ISNA(xd[i]) ? naval : nanval;
758758
}
759759
} else {
760760
elem = dtwiddle(xd[i]); // TODO: could avoid twiddle() if all positive finite which could be known from range_d.
761-
// also R_FINITE is repeated within dtwiddle() currently, wastefully given the if() above
761+
// also isfinite is repeated within dtwiddle() currently, wastefully given the if() above
762762
}
763763
WRITE_KEY
764764
}

src/freadR.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ SEXP freadR(
127127
args.nrowLimit = INT64_MAX;
128128
if (!isReal(nrowLimitArg) || length(nrowLimitArg) != 1)
129129
internal_error(__func__, "nrows not a single real. R level catches this."); // # nocov
130-
if (R_FINITE(REAL(nrowLimitArg)[0]) && REAL(nrowLimitArg)[0] >= 0.0)
130+
if (isfinite(REAL(nrowLimitArg)[0]) && REAL(nrowLimitArg)[0] >= 0.0)
131131
args.nrowLimit = (int64_t)(REAL(nrowLimitArg)[0]);
132132

133133
args.logical01 = LOGICAL(logical01Arg)[0];

src/froll.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ void frollmeanFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool
6565
}
6666
w += x[i]; // i==k-1
6767
ans->dbl_v[i] = (double) (w / k); // first full sliding window, non-fill rollfun answer
68-
if (R_FINITE((double) w)) { // proceed only if no NAs detected in first k obs, otherwise early stopping
68+
if (isfinite((double) w)) { // proceed only if no NAs detected in first k obs, otherwise early stopping
6969
for (uint64_t i=k; i<nx; i++) { // loop over obs, complete window, all remaining after partial window
7070
w -= x[i-k]; // remove leaving row from sliding window
7171
w += x[i]; // add current row to sliding window
7272
ans->dbl_v[i] = (double) (w / k); // rollfun to answer vector
7373
}
74-
if (!R_FINITE((double) w)) { // mark to re-run with NA care
74+
if (!isfinite((double) w)) { // mark to re-run with NA care
7575
if (hasna==-1) { // raise warning
7676
ans->status = 2;
7777
snprintf(end(ans->message[2]), 500, _("%s: hasNA=FALSE used but NA (or other non-finite) value(s) are present in input, use default hasNA=NA to avoid this warning"), __func__);
@@ -96,14 +96,14 @@ void frollmeanFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool
9696
int nc = 0; // NA counter within sliding window
9797
int i; // iterator declared here because it is being used after for loop
9898
for (i=0; i<k-1; i++) { // loop over leading observation, all partial window only; #loop_counter_not_local_scope_ok
99-
if (R_FINITE(x[i])) {
99+
if (isfinite(x[i])) {
100100
w += x[i]; // add only finite values to window aggregate
101101
} else {
102102
nc++; // increment NA count in current window
103103
}
104104
ans->dbl_v[i] = fill; // partial window fill all
105105
}
106-
if (R_FINITE(x[i])) {
106+
if (isfinite(x[i])) {
107107
w += x[i]; // i==k-1
108108
} else {
109109
nc++;
@@ -116,12 +116,12 @@ void frollmeanFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool
116116
ans->dbl_v[i] = narm ? (double) (w / (k - nc)) : NA_REAL; // some values in window are NA
117117
}
118118
for (uint64_t i=k; i<nx; i++) { // loop over obs, complete window, all remaining after partial window
119-
if (R_FINITE(x[i])) {
119+
if (isfinite(x[i])) {
120120
w += x[i]; // add only finite to window aggregate
121121
} else {
122122
nc++; // increment NA count in current window
123123
}
124-
if (R_FINITE(x[i-k])) {
124+
if (isfinite(x[i-k])) {
125125
w -= x[i-k]; // remove only finite from window aggregate
126126
} else {
127127
nc--; // decrement NA count in current window
@@ -158,7 +158,7 @@ void frollmeanExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool
158158
for (int j=-k+1; j<=0; j++) { // sub-loop on window width
159159
w += x[i+j]; // sum of window for particular observation
160160
}
161-
if (R_FINITE((double) w)) { // no need to calc roundoff correction if NAs detected as will re-call all below in truehasna==1
161+
if (isfinite((double) w)) { // no need to calc roundoff correction if NAs detected as will re-call all below in truehasna==1
162162
long double res = w / k; // keep results as long double for intermediate processing
163163
long double err = 0.0; // roundoff corrector
164164
for (int j=-k+1; j<=0; j++) { // nested loop on window width
@@ -268,13 +268,13 @@ void frollsumFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool n
268268
}
269269
w += x[i];
270270
ans->dbl_v[i] = (double) w;
271-
if (R_FINITE((double) w)) {
271+
if (isfinite((double) w)) {
272272
for (uint64_t i=k; i<nx; i++) {
273273
w -= x[i-k];
274274
w += x[i];
275275
ans->dbl_v[i] = (double) w;
276276
}
277-
if (!R_FINITE((double) w)) {
277+
if (!isfinite((double) w)) {
278278
if (hasna==-1) {
279279
ans->status = 2;
280280
snprintf(end(ans->message[2]), 500, _("%s: hasNA=FALSE used but NA (or other non-finite) value(s) are present in input, use default hasNA=NA to avoid this warning"), __func__);
@@ -299,14 +299,14 @@ void frollsumFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool n
299299
int nc = 0;
300300
int i;
301301
for (i=0; i<k-1; i++) { // #loop_counter_not_local_scope_ok
302-
if (R_FINITE(x[i])) {
302+
if (isfinite(x[i])) {
303303
w += x[i];
304304
} else {
305305
nc++;
306306
}
307307
ans->dbl_v[i] = fill;
308308
}
309-
if (R_FINITE(x[i])) {
309+
if (isfinite(x[i])) {
310310
w += x[i];
311311
} else {
312312
nc++;
@@ -319,12 +319,12 @@ void frollsumFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool n
319319
ans->dbl_v[i] = narm ? (double) w : NA_REAL;
320320
}
321321
for (uint64_t i=k; i<nx; i++) {
322-
if (R_FINITE(x[i])) {
322+
if (isfinite(x[i])) {
323323
w += x[i];
324324
} else {
325325
nc++;
326326
}
327-
if (R_FINITE(x[i-k])) {
327+
if (isfinite(x[i-k])) {
328328
w -= x[i-k];
329329
} else {
330330
nc--;
@@ -356,7 +356,7 @@ void frollsumExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool
356356
for (int j=-k+1; j<=0; j++) {
357357
w += x[i+j];
358358
}
359-
if (R_FINITE((double) w)) {
359+
if (isfinite((double) w)) {
360360
ans->dbl_v[i] = (double) w;
361361
} else {
362362
if (!narm) {

src/frolladaptive.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void fadaptiverollmeanFast(double *x, uint64_t nx, ans_t *ans, int *k, double fi
4141
w += x[i]; // cumulate in long double
4242
cs[i] = (double) w;
4343
}
44-
if (R_FINITE((double) w)) { // no need to calc this if NAs detected as will re-calc all below in truehasna==1
44+
if (isfinite((double) w)) { // no need to calc this if NAs detected as will re-calc all below in truehasna==1
4545
#pragma omp parallel for num_threads(getDTthreads(nx, true))
4646
for (uint64_t i=0; i<nx; i++) { // loop over observations to calculate final answer
4747
if (i+1 == k[i]) {
@@ -74,7 +74,7 @@ void fadaptiverollmeanFast(double *x, uint64_t nx, ans_t *ans, int *k, double fi
7474
return;
7575
} // # nocov end
7676
for (uint64_t i=0; i<nx; i++) { // loop over observations to calculate cumsum and cum NA counter
77-
if (R_FINITE(x[i])) {
77+
if (isfinite(x[i])) {
7878
w += x[i]; // add observation to running sum
7979
} else {
8080
nc++; // increment non-finite counter
@@ -126,7 +126,7 @@ void fadaptiverollmeanExact(double *x, uint64_t nx, ans_t *ans, int *k, double f
126126
for (int j=-k[i]+1; j<=0; j++) { // sub-loop on window width
127127
w += x[i+j]; // sum of window for particular observation
128128
}
129-
if (R_FINITE((double) w)) { // no need to calc roundoff correction if NAs detected as will re-call all below in truehasna==1
129+
if (isfinite((double) w)) { // no need to calc roundoff correction if NAs detected as will re-call all below in truehasna==1
130130
long double res = w / k[i]; // keep results as long double for intermediate processing
131131
long double err = 0.0; // roundoff corrector
132132
for (int j=-k[i]+1; j<=0; j++) { // sub-loop on window width
@@ -230,7 +230,7 @@ void fadaptiverollsumFast(double *x, uint64_t nx, ans_t *ans, int *k, double fil
230230
w += x[i];
231231
cs[i] = (double) w;
232232
}
233-
if (R_FINITE((double) w)) {
233+
if (isfinite((double) w)) {
234234
#pragma omp parallel for num_threads(getDTthreads(nx, true))
235235
for (uint64_t i=0; i<nx; i++) {
236236
if (i+1 == k[i]) {
@@ -263,7 +263,7 @@ void fadaptiverollsumFast(double *x, uint64_t nx, ans_t *ans, int *k, double fil
263263
return;
264264
} // # nocov end
265265
for (uint64_t i=0; i<nx; i++) {
266-
if (R_FINITE(x[i])) {
266+
if (isfinite(x[i])) {
267267
w += x[i];
268268
} else {
269269
nc++;
@@ -310,7 +310,7 @@ void fadaptiverollsumExact(double *x, uint64_t nx, ans_t *ans, int *k, double fi
310310
for (int j=-k[i]+1; j<=0; j++) {
311311
w += x[i+j];
312312
}
313-
if (R_FINITE((double) w)) {
313+
if (isfinite((double) w)) {
314314
ans->dbl_v[i] = (double) w;
315315
} else {
316316
if (!narm) {

src/utils.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ bool within_int32_repres(double x) {
44
// N.B. (int)2147483647.99 is not undefined behaviour since s 6.3.1.4 of the C
55
// standard states that behaviour is undefined only if the integral part of a
66
// finite value of standard floating type cannot be represented.
7-
return R_FINITE(x) && x < 2147483648 && x > -2147483648;
7+
return isfinite(x) && x < 2147483648 && x > -2147483648;
88
}
99

1010
bool within_int64_repres(double x) {
11-
return R_FINITE(x) && x <= (double)INT64_MAX && x >= (double)INT64_MIN;
11+
return isfinite(x) && x <= (double)INT64_MAX && x >= (double)INT64_MIN;
1212
}
1313

1414
// used to error if not passed type double but this needed extra is.double() calls in calling R code

0 commit comments

Comments
 (0)