Subject: Re: bug#14650: coreutils' getlimits fails to represent float limits correctly

From: Pádraig Brady <[email-masked]>
Date: Tue, 18 Jun 2013 10:29:32 +0100

On 06/18/2013 08:47 AM, Paul Eggert wrote:
> On 06/17/2013 11:10 PM, [email-masked] wrote:
>> I'm not aware of a convenient way to compute the number
>> of digits
> 
> coreutils already can do that; see lib/ftoastr.h.
> Presumably coreutils should use the ftoastr module hear.

Something like the patch below?
which changes the output from:

 FLT_MIN=1.175494e-38
 FLT_MAX=3.402823e+38
 DBL_MIN=2.225074e-308
 DBL_MAX=1.797693e+308
 LDBL_MIN=3.362103e-4932
 LDBL_MAX=1.189731e+4932

to...

 FLT_MIN=1.1754944e-38
 FLT_MAX=3.4028235e+38
 DBL_MIN=2.2250738585072014e-308
 DBL_MAX=1.7976931348623157e+308
 LDBL_MIN=3.3621031431120935063e-4932
 LDBL_MAX=1.189731495357231765e+4932

Note the difference lengths of LDBL_MIN and LDBL_MAX.

cheers,
Pádraig.

diff --git a/src/getlimits.c b/src/getlimits.c
index 7c1fbe2..dfc4b12 100644
--- a/src/getlimits.c
+++ b/src/getlimits.c
@@ -21,6 +21,7 @@
 #include <sys/types.h>
 #include <float.h>

+#include "ftoastr.h"
 #include "system.h"
 #include "long-options.h"

@@ -97,6 +98,19 @@ decimal_absval_add_one (char *buf)
   return result;
 }

+#define PRINT_FLOATTYPE(N, T, FTOASTR, BUFSIZE)                         \
+static void                                                             \
+N (T x)                                                                 \
+{                                                                       \
+  char buf[BUFSIZE];                                                    \
+  FTOASTR (buf, sizeof buf, FTOASTR_LEFT_JUSTIFY, 0, x);                \
+  puts (buf);                                                           \
+}
+
+PRINT_FLOATTYPE (print_FLT, float, ftoastr, FLT_BUFSIZE_BOUND)
+PRINT_FLOATTYPE (print_DBL, double, dtoastr, DBL_BUFSIZE_BOUND)
+PRINT_FLOATTYPE (print_LDBL, long double, ldtoastr, LDBL_BUFSIZE_BOUND)
+
 int
 main (int argc, char **argv)
 {
@@ -127,8 +141,8 @@ main (int argc, char **argv)
     }

 #define print_float(TYPE)                                                \
-  printf (#TYPE"_MIN=%Le\n", (long double)TYPE##_MIN);                   \
-  printf (#TYPE"_MAX=%Le\n", (long double)TYPE##_MAX);
+  printf (#TYPE"_MIN="); print_##TYPE(TYPE##_MIN);                       \
+  printf (#TYPE"_MAX="); print_##TYPE(TYPE##_MAX);

   /* Variable sized ints */
   print_int (CHAR);