[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Bug#208494: coreutils: seq -w doesn't work properly with two negativ
From: |
Paul Eggert |
Subject: |
Re: Bug#208494: coreutils: seq -w doesn't work properly with two negative values |
Date: |
04 Sep 2003 14:58:45 -0700 |
User-agent: |
Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 |
> Patrick Mauritz <address@hidden> wrote:
> > seq -w 0 -11 returns
> > 000
> > -01
> > ...
> > -11
Thanks, but isn't there another bug here? The default increment is 1,
which means that seq 0 -11 should output nothing at all.
The current seq code automatically defaults the increment to be -1 if
the lower bound is less than the upper bound. This sounds wrong to
me. If I issue the command 'seq 0', I expect to get zero lines of
output, not two. The increment should default to 1, as the
documentation says.
Here's a proposed patch.
2003-09-04 Paul Eggert <address@hidden>
* src/seq.c (step): Default to 1.
(print_numbers): Allow the output to be empty.
(main): The default step is 1, even if LAST < FIRST;
as per documentation.
* tests/seq/basic (onearg-2): Output should be empty.
Index: src/seq.c
===================================================================
RCS file: /cvsroot/coreutils/coreutils/src/seq.c,v
retrieving revision 1.71
diff -p -u -r1.71 seq.c
--- src/seq.c 23 Jul 2003 07:29:55 -0000 1.71
+++ src/seq.c 4 Sep 2003 21:31:39 -0000
@@ -55,7 +55,7 @@ static char *decimal_point = ".";
static double first;
/* The increment. */
-static double step;
+static double step = 1.0;
/* The last number. */
static double last;
@@ -178,55 +178,20 @@ valid_format (const char *fmt)
static int
print_numbers (const char *fmt)
{
- if (first > last)
- {
- int i;
-
- if (step >= 0)
- {
- error (0, 0,
- _("when the starting value is larger than the limit,\n\
-the increment must be negative"));
- usage (EXIT_FAILURE);
- }
-
- printf (fmt, first);
- for (i = 1; /* empty */; i++)
- {
- double x = first + i * step;
+ int i;
- if (x < last)
- break;
-
- fputs (separator, stdout);
- printf (fmt, x);
- }
- }
- else
+ for (i = 0; /* empty */; i++)
{
- int i;
-
- if (step <= 0)
- {
- error (0, 0,
- _("when the starting value is smaller than the limit,\n\
-the increment must be positive"));
- usage (EXIT_FAILURE);
- }
-
- printf (fmt, first);
- for (i = 1; /* empty */; i++)
- {
- double x = first + i * step;
-
- if (x > last)
- break;
-
- fputs (separator, stdout);
- printf (fmt, x);
- }
+ double x = first + i * step;
+ if (step < 0 ? x < last : last < x)
+ break;
+ if (i)
+ fputs (separator, stdout);
+ printf (fmt, x);
}
- fputs (terminator, stdout);
+
+ if (i)
+ fputs (terminator, stdout);
return 0;
}
@@ -333,7 +298,6 @@ main (int argc, char **argv)
{
int errs;
int optc;
- int step_is_set;
/* The printf(3) format used for output. */
char *format_str = NULL;
@@ -349,7 +313,6 @@ main (int argc, char **argv)
equal_width = 0;
separator = "\n";
first = 1.0;
- step_is_set = 0;
/* Figure out the locale's idea of a decimal point. */
#if HAVE_LOCALECONV
@@ -434,9 +397,7 @@ main (int argc, char **argv)
if (optind < argc)
{
step = last;
- step_is_set = 1;
last = scan_double_arg (argv[optind++]);
-
}
}
@@ -445,11 +406,6 @@ main (int argc, char **argv)
error (0, 0, _("\
format string may not be specified when printing equal width strings"));
usage (EXIT_FAILURE);
- }
-
- if (!step_is_set)
- {
- step = first <= last ? 1.0 : -1.0;
}
if (format_str == NULL)
Index: tests/seq/basic
===================================================================
RCS file: /cvsroot/coreutils/coreutils/tests/seq/basic,v
retrieving revision 1.8
diff -p -u -r1.8 basic
--- tests/seq/basic 8 Apr 2003 10:55:01 -0000 1.8
+++ tests/seq/basic 4 Sep 2003 21:31:44 -0000
@@ -25,7 +25,7 @@ my $prog = $ENV{PROG} || die "$0: \$PROG
my @Tests =
(
['onearg-1', qw(10), {OUT => [(1..10)]}],
- ['onearg-2', qw(-1), {OUT => [qw(1 0 -1)]}],
+ ['onearg-2', qw(-1)],
['neg-1', qw(-10 10 10), {OUT => [qw(-10 0 10)]}],
['neg-2', qw(-.1 .1 .1), {OUT => [qw(-0.1 0 0.1)]}],
['neg-3', qw(1 -1 0), {OUT => [qw(1 0)]}],