Subject: Re: bug#12959: "seq -w -1e-3 9" misaligned
From: Pádraig Brady <[email-masked]>
To: Marcel Böhme <[email-masked]>
Cc: [email-masked]
Date: Fri, 23 Nov 2012 09:04:54 +0000
List-Id: <debbugs-submit.debbugs.gnu.org>

On 11/22/2012 10:58 AM, Pádraig Brady wrote:
> On 11/22/2012 10:49 AM, Marcel Böhme wrote:
>> Hi,
>>
>> While the output of (1) "seq -w -1e-2 9" prints the width as expected, the output of (2) "seq -w -1e-3 9" does not:
>>   (1) vs. (2)
>> -0.01 | -0.001
>> 00.99 | 0.999
>> 01.99 | 1.999
>> 02.99 | 2.999
>> 03.99 | 3.999
>> 04.99 | 4.999
>> 05.99 | 5.999
>> 06.99 | 6.999
>> 07.99 | 7.999
>> 08.99 | 8.999
>>
>> Similarly, see "seq -w -1e2 -98" vs. "seq -w -1e3 -998".
>>
>> Can you kindly confirm that the bug was introduced 4 years ago in the following commit: http://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=commitdiff;h=86e4b778b148bdd82395fdc312ce8d937f303e33
>
> Confirmed:
>
> $ ~/git/coreutils/src/seq -w -1e-3 1
> -0.001
> 0.999
>
> $ ~/git/coreutils/src/seq -w -.001 1
> -0.001
> 00.999

The attached should fix this.

thanks,
Pádraig.
From f324be2ff77c97d9551433d1c5fdbb3d791f7ed2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email-masked]>
Date: Fri, 23 Nov 2012 03:06:07 +0000
Subject: [PATCH] seq: ensure correct width output for scientific notation
 input
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* src/seq.c (scan_arg): Calculate the width more accurately
for numbers specified using scientific notation.
* tests/misc/seq.pl: Add test cases for cases that were mishandled
* NEWS: Mention the fix.
* THANKS.in: Reported by Marcel Böhme
---
 NEWS              |    4 ++++
 THANKS.in         |    1 +
 src/seq.c         |   15 +++++++++++++++
 tests/misc/seq.pl |    6 ++++++
 4 files changed, 26 insertions(+), 0 deletions(-)

diff --git a/NEWS b/NEWS
index 15fddd4..8529216 100644
--- a/NEWS
+++ b/NEWS
@@ -21,6 +21,10 @@ GNU coreutils NEWS                                    -*- outline -*-
   consistently padded with spaces, rather than with zeros for certain widths.
   [bug introduced in TEXTUTILS-1_22i]
 
+  seq -w ensures that for numbers input in scientific notation,
+  the output numbers are properly aligned and of the correct width.
+  [This bug was present in "the beginning".]
+
 ** Changes in behavior
 
   df --total now prints '-' into the target column (mount point) of the
diff --git a/THANKS.in b/THANKS.in
index 016a41e..3080cd3 100644
--- a/THANKS.in
+++ b/THANKS.in
@@ -367,6 +367,7 @@ Marc Haber                          [email-masked]
 Marc Mengel                         [email-masked]
 Marc Lehman                         [email-masked]
 Marc Olzheim                        [email-masked]
+Marcel Böhme                        [email-masked]
 Marco Franzen                       [email-masked]
 Marcus Brinkmann                    http://www.marcus-brinkmann.de
 Marcus Daniels                      [email-masked]
diff --git a/src/seq.c b/src/seq.c
index e5788ca..9c2c51f 100644
--- a/src/seq.c
+++ b/src/seq.c
@@ -166,6 +166,21 @@ scan_arg (const char *arg)
         {
           long exponent = strtol (e + 1, NULL, 10);
           ret.precision += exponent < 0 ? -exponent : 0;
+          /* Don't account for e.... in the width since this is not output.  */
+          ret.width -= strlen (arg) - (e - arg);
+          /* Adjust the width as per the exponent.  */
+          if (exponent < 0)
+            {
+              if (decimal_point)
+                {
+                  if (e == decimal_point + 1) /* undo #. -> # above  */
+                    ret.width++;
+                }
+              else
+                ret.width++;
+              exponent = -exponent;
+            }
+          ret.width += exponent;
         }
     }
 
diff --git a/tests/misc/seq.pl b/tests/misc/seq.pl
index 351097b..39e5528 100755
--- a/tests/misc/seq.pl
+++ b/tests/misc/seq.pl
@@ -70,6 +70,12 @@ my @Tests =
    ['eq-wid-6',	qw(-w +1 2),  {OUT => [qw(1 2)]}],
    ['eq-wid-7',	qw(-w "    .1"  "    .1"),  {OUT => [qw(0.1)]}],
    ['eq-wid-8',	qw(-w 9 0.5 10),  {OUT => [qw(09.0 09.5 10.0)]}],
+   # Prior to 8.21, these tests involving numbers in scentific notation
+   # would fail with misalignment or wrong widths.
+   ['eq-wid-9',	qw(-w -1e-3 1),  {OUT => [qw(-0.001 00.999)]}],
+   ['eq-wid-10',qw(-w -1e-003 1),  {OUT => [qw(-0.001 00.999)]}],
+   ['eq-wid-11',qw(-w -1.e-3 1),  {OUT => [qw(-0.001 00.999)]}],
+   ['eq-wid-12',qw(-w -1.0e-4 1),  {OUT => [qw(-0.00010 00.99990)]}],
 
    # Prior to coreutils-4.5.11, some of these were not accepted.
    ['fmt-1',	qw(-f %2.1f 1.5 .5 2),{OUT => [qw(1.5 2.0)]}],
-- 
1.7.6.4