Skip to content

Commit abbebd3

Browse files
author
maechler
committed
seq.int(from, to, by=+/-1) ==> from:to
git-svn-id: https://svn.r-project.org/R/trunk@87507 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 2f796b4 commit abbebd3

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

doc/NEWS.Rd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@
144144
\item If \code{La_library()} is empty, \code{sessionInfo()} still
145145
reports \code{La_version()} when available.
146146

147+
\item \code{seq.int(from, to, by, ....)} when \eqn{|by| = 1} now
148+
behaves as if \code{by} was omitted, and hence returns \code{from:to},
149+
possibly integer.
150+
147151
\item \code{seq.Date(from, to, by, ....)} and \code{seq.POSIXt(..)}
148152
now also work when \code{from} is missing and sufficient further
149153
arguments are provided, thanks to \I{Michael Chirico}'s report, patch

src/main/seq.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* R : A Computer Language for Statistical Data Analysis
3-
* Copyright (C) 1998-2023 The R Core Team.
3+
* Copyright (C) 1998-2025 The R Core Team.
44
* Copyright (C) 1995-1998 Robert Gentleman and Ross Ihaka
55
*
66
* This program is free software; you can redistribute it and/or modify
@@ -109,7 +109,7 @@ static SEXP seq_colon(double n1, double n2, SEXP call)
109109

110110
Rboolean useInt = (n1 <= INT_MAX) && (n1 == (int) n1);
111111
if(useInt) {
112-
if(n1 <= INT_MIN || n1 > INT_MAX)
112+
if(n1 <= INT_MIN) /* know n1 <= INT_MAX */
113113
useInt = FALSE;
114114
else {
115115
/* r := " the effective 'to' " of from:to */
@@ -787,11 +787,12 @@ attribute_hidden SEXP do_rep(SEXP call, SEXP op, SEXP args, SEXP rho)
787787

788788

789789
/*
790-
This is a primitive SPECIALSXP with internal argument matching,
791-
implementing seq.int().
790+
This is a primitive SPECIALSXP with internal argument matching, implementing
792791
793-
'along' has to be used on an unevaluated argument, and evalList
794-
tries to evaluate language objects.
792+
seq.int(from, to, by, length.out, along.with, ...)
793+
794+
'along' has to be used on an unevaluated argument, and evalList
795+
tries to evaluate language objects.
795796
*/
796797
#define FEPS 1e-10
797798
/* to match seq.default */
@@ -832,7 +833,7 @@ attribute_hidden SEXP do_seq(SEXP call, SEXP op, SEXP args, SEXP rho)
832833
errorcall(call, _("'%s' must be a finite number"), "from");
833834
ans = seq_colon(1.0, rfrom, call);
834835
}
835-
else if (lf)
836+
else if (lf) // typically seq(<vec>) , length(<vec>) >= 2
836837
ans = seq_colon(1.0, (double)lf, call);
837838
else
838839
ans = allocVector(INTSXP, 0);
@@ -884,13 +885,16 @@ attribute_hidden SEXP do_seq(SEXP call, SEXP op, SEXP args, SEXP rho)
884885
ans = to; // is *not* missing in this case
885886
goto done;
886887
}
887-
double n, rby = asReal(by);
888-
Rboolean finite_del = R_FINITE(del);
889-
if(finite_del) {
890-
n = del/rby;
891-
} else { // overflow in (to - from) when both are finite
892-
n = rto/rby - rfrom/rby;
888+
double rby = asReal(by);
889+
if((rby == 1. && del > 0.) ||
890+
(rby == -1. && del < 0.)) { // --> treat as if missing (return integer)
891+
ans = seq_colon(rfrom, rto, call);
892+
goto done;
893893
}
894+
Rboolean finite_del = R_FINITE(del);
895+
double n = (finite_del)
896+
? del/rby
897+
: rto/rby - rfrom/rby; /* overflow in (to - from) when both are finite */
894898
if(!R_FINITE(n)) {
895899
if(del == 0.0 && rby == 0.0) {
896900
ans = miss_from ? ScalarReal(rfrom) : from;
@@ -1047,7 +1051,7 @@ attribute_hidden SEXP do_seq(SEXP call, SEXP op, SEXP args, SEXP rho)
10471051
done:
10481052
UNPROTECT(1);
10491053
return ans;
1050-
}
1054+
} // do_seq()
10511055

10521056
attribute_hidden SEXP do_seq_along(SEXP call, SEXP op, SEXP args, SEXP rho)
10531057
{

tests/reg-tests-1d.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ stopifnot(exprs = {
644644
## had failed in R-devel for a few days
645645
D1 <- as.Date("2017-01-06")
646646
D2 <- as.Date("2017-01-12")
647-
seqD1 <- seq.Date(D1, D2, by = "1 day")
647+
seqD1 <- seq(D1, D2, by = "1 day")
648648
stopifnot(exprs = {
649649
identical(seqD1, seq(D1, D2)) # by = "days" implicit default since R >= 4.5
650650
identical(seqD1, seq(D1, D2, by = "1 days"))

0 commit comments

Comments
 (0)