From ce1b667ab7c38cb2633fca0075652a69e5d2a343 Mon Sep 17 00:00:00 2001 From: Joshua Ulrich Date: Sat, 18 Jun 2016 11:35:42 -0500 Subject: [PATCH] Correct endpoints when index is before the epoch The endpoints C code casts the double index to long, which truncates it toward zero. This behavior is desired when the index is positive, because it moves the endpoint *back* in time. But when the index is negative, truncating toward zero moves the endpoint *forward* in time. This is also an issue if the index value is stored as integer, since the C99 specification states that integer division truncates toward zero. If the first index value is less than zero, branch into a special case to handle pre-epoch index values. This avoids performance degradation if all index values are after the epoch. If the index value is less than zero, simply add 1 to offset the truncation toward zero. We also need to furthre adjust the potential endpoint value if the index is exactly equal to zero. Fixes #144. --- src/endpoints.c | 72 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 14 deletions(-) diff --git a/src/endpoints.c b/src/endpoints.c index 9c8aa50b..a5a70275 100644 --- a/src/endpoints.c +++ b/src/endpoints.c @@ -36,26 +36,70 @@ SEXP endpoints (SEXP _x, SEXP _on, SEXP _k, SEXP _addlast /* TRUE */) /*int_index = INTEGER(getAttrib(_x, install("index")));*/ int_index = INTEGER(_x); ep[0] = 0; - for(i=1,j=1; i