I have observed an unexpected behavior in seq(1),
that is related to the general lack of absolute accuracy
of floating point values.
The seq output may vary with different systems or compilers
and does not always print all numbers from FIRST to LAST.
Here is a screen dump on Linux-2.2.19 (i386):
juergen@anna:~ > seq -s " " 0 .1 1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
juergen@anna:~ > seq -s " " 0 .1 1.1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1
juergen@anna:~ > seq -s " " 0 .1 1.2
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1
juergen@anna:~ > seq -s " " 0 .1 1.3
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 1.2
juergen@anna:~ > seq -s " " 0 .1 1.4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 1.2 1.3
And this is a screen dump on Solaris 8 (sparc)
lyra$ seq -s " " 0 .1 1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
lyra$ seq -s " " 0 .1 1.1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1
lyra$ seq -s " " 0 .1 1.2
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1
lyra$ seq -s " " 0 .1 1.3
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 1.2 1.3
lyra$ seq -s " " 0 .1 1.4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 1.2 1.3
Please find attached a suggested patch (unified diff) to
overcome this problem.
Best regards - Juergen Salk
--- seq.c.orig Fri Sep 14 22:42:54 2001
+++ seq.c Fri Sep 14 22:42:57 2001
@@ -414,6 +414,7 @@
if (first > last)
{
int i;
+ double threshold;
if (step >= 0)
{
@@ -425,11 +426,12 @@
}
printf (fmt, first);
+ threshold = last - 0.5 * step;
for (i = 1; /* empty */; i++)
{
double x = first + i * step;
- if (x < last)
+ if (x < threshold)
break;
fputs (separator, stdout);
@@ -439,6 +441,7 @@
else
{
int i;
+ double threshold;
if (step <= 0)
{
@@ -450,11 +453,12 @@
}
printf (fmt, first);
+ threshold = last + 0.5 * step;
for (i = 1; /* empty */; i++)
{
double x = first + i * step;
- if (x > last)
+ if (x > threshold)
break;
fputs (separator, stdout);