summaryrefslogtreecommitdiff
path: root/src/seq.c
diff options
context:
space:
mode:
authorJim Meyering <meyering@redhat.com>2008-04-14 11:21:26 +0200
committerJim Meyering <meyering@redhat.com>2008-04-14 11:29:20 +0200
commit4827dd27b0c655a685947aaa01426a5ecba179f3 (patch)
tree6270e3d5bb3eb832ed3d39a312882221b95e52e8 /src/seq.c
parentf91dec277a0d52489dd58a9f1c9eb4fd54fca7b8 (diff)
downloadcoreutils-4827dd27b0c655a685947aaa01426a5ecba179f3.tar.xz
seq: work around floating point inaccuracies on more systems
* src/seq.c: Include <math.h> for fabs. Include <float.h> for DBL_EPSILON. (abs_rel_diff): New function. (print_numbers): Use abs_rel_diff rather than a strict equality test. Without this change, Solaris 8 and Irix 6.2 would fail the float-6 test. Reported by Peter Fales in http://thread.gmane.org/gmane.comp.gnu.coreutils.bugs/13183 * src/c99-to-c89.diff: Adjust seq.c offsets.
Diffstat (limited to 'src/seq.c')
-rw-r--r--src/seq.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/seq.c b/src/seq.c
index 64bbeee00..7fc89ad5d 100644
--- a/src/seq.c
+++ b/src/seq.c
@@ -20,6 +20,8 @@
#include <getopt.h>
#include <stdio.h>
#include <sys/types.h>
+#include <math.h>
+#include <float.h>
#include "system.h"
#include "c-strtod.h"
@@ -259,6 +261,14 @@ long_double_format (char const *fmt, struct layout *layout)
return NULL;
}
+/* Return the absolute relative difference from x to y. */
+static double
+abs_rel_diff (double x, double y)
+{
+ double s = (y == 0.0 ? 1 : y);
+ return fabs ((y - x) / s);
+}
+
/* Actually print the sequence of numbers in the specified range, with the
given or default stepping and format. */
@@ -300,7 +310,7 @@ print_numbers (char const *fmt, struct layout layout,
x_str[x_strlen - layout.suffix_len] = '\0';
if (xstrtold (x_str + layout.prefix_len, NULL, &x_val, c_strtold)
- && x_val == last)
+ && abs_rel_diff (x_val, last) < DBL_EPSILON)
{
char *x0_str = NULL;
if (asprintf (&x0_str, fmt, x0) < 0)