diff options
author | Pádraig Brady <P@draigBrady.com> | 2010-04-28 23:54:33 +0100 |
---|---|---|
committer | Pádraig Brady <P@draigBrady.com> | 2010-04-29 18:55:57 +0100 |
commit | 8b5087d4e62439503e3cba774de225893b9ea81a (patch) | |
tree | 7ac7a407ac5368ca46082a807f17c16d009ded31 /src | |
parent | 7905d6d34dbeb73ef03b3c75f66dde9036634775 (diff) | |
download | coreutils-8b5087d4e62439503e3cba774de225893b9ea81a.tar.xz |
sort: use long doubles for general numeric mode
* src/sort.c (general_numcompare): Use long doubles unconditionally,
and strtold when available, to convert numbers with greater range and
precision. Performance was seen to be on par with standard doubles.
* doc/coreutils.texi (sort invocation): Amend the -g description to
mention long double rather than double, and strtold rather than strtod.
* src/getlimits.c (main): Output floating point limits for use in tests.
* tests/misc/sort-float: A new test to ensure sort is using long
doubles when possible, and that locale specific floats are handled.
* tests/Makefile.am: Reference the new test.
* tests/test-lib.sh (getlimits_): Normalize indenting.
* NEWS: Mention the new behaviour.
Reported by Nelson Beebe.
Diffstat (limited to 'src')
-rw-r--r-- | src/getlimits.c | 16 | ||||
-rw-r--r-- | src/sort.c | 10 |
2 files changed, 21 insertions, 5 deletions
diff --git a/src/getlimits.c b/src/getlimits.c index 48d07b5ae..93d4035a7 100644 --- a/src/getlimits.c +++ b/src/getlimits.c @@ -19,6 +19,7 @@ #include <config.h> /* sets _FILE_OFFSET_BITS=64 etc. */ #include <stdio.h> #include <sys/types.h> +#include <float.h> #include "system.h" #include "c-ctype.h" @@ -123,7 +124,7 @@ decimal_ascii_add (const char *str1, const char *str2) int main (int argc, char **argv) { - char limit[64]; /* big enough for 128 bit at least */ + char limit[64]; /* big enough for 128 bit integers at least */ char *oflow; initialize_main (&argc, &argv); @@ -139,20 +140,24 @@ main (int argc, char **argv) usage, AUTHORS, (char const *) NULL); #define print_int(TYPE) \ - snprintf (limit, sizeof limit, "%"PRIuMAX, (uintmax_t)TYPE##_MAX); \ + snprintf (limit, sizeof limit, "%"PRIuMAX, (uintmax_t)TYPE##_MAX); \ printf (#TYPE"_MAX=%s\n", limit); \ oflow = decimal_ascii_add (limit, "1"); \ printf (#TYPE"_OFLOW=%s\n", oflow); \ free (oflow); \ if (TYPE##_MIN) \ { \ - snprintf (limit, sizeof limit, "%"PRIdMAX, (intmax_t)TYPE##_MIN); \ + snprintf (limit, sizeof limit, "%"PRIdMAX, (intmax_t)TYPE##_MIN); \ printf (#TYPE"_MIN=%s\n", limit); \ oflow = decimal_ascii_add (limit, "-1"); \ printf (#TYPE"_UFLOW=%s\n", oflow); \ free (oflow); \ } +#define print_float(TYPE) \ + printf (#TYPE"_MIN=%Le\n", (long double)TYPE##_MIN); \ + printf (#TYPE"_MAX=%Le\n", (long double)TYPE##_MAX); + /* Variable sized ints */ print_int (CHAR); print_int (SCHAR); @@ -171,4 +176,9 @@ main (int argc, char **argv) print_int (OFF_T); print_int (INTMAX); print_int (UINTMAX); + + /* Variable sized floats */ + print_float (FLT); + print_float (DBL); + print_float (LDBL); } diff --git a/src/sort.c b/src/sort.c index 6d47b794b..a81524470 100644 --- a/src/sort.c +++ b/src/sort.c @@ -1855,10 +1855,16 @@ general_numcompare (const char *sa, const char *sb) /* FIXME: maybe add option to try expensive FP conversion only if A and B can't be compared more cheaply/accurately. */ +#if HAVE_C99_STRTOLD /* provided by c-strtold module. */ +# define STRTOD strtold +#else +# define STRTOD strtod +#endif + char *ea; char *eb; - double a = strtod (sa, &ea); - double b = strtod (sb, &eb); + long double a = STRTOD (sa, &ea); + long double b = STRTOD (sb, &eb); /* Put conversion errors at the start of the collating sequence. */ if (sa == ea) |