]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Use version fromglibc 2.1 which corrects several bugs.
authorUlrich Drepper <drepper@redhat.com>
Wed, 19 Nov 1997 23:03:33 +0000 (23:03 +0000)
committerUlrich Drepper <drepper@redhat.com>
Wed, 19 Nov 1997 23:03:33 +0000 (23:03 +0000)
misc/efgcvt.c
misc/efgcvt_r.c

index 24d63176d03071d126122bde973e5fb81732135c..4db0d84902c2934222ce3011fc6abdfde4f4f879 100644 (file)
@@ -39,7 +39,8 @@ APPEND (FUNC_PREFIX, fcvt) (value, ndigit, decpt, sign)
 {
   static char buf[MAXDIG];
 
-  (void) fcvt_r (value, ndigit, decpt, sign, buf, sizeof buf);
+  (void) APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign,
+                                      buf, sizeof buf);
 
   return buf;
 }
@@ -51,7 +52,8 @@ APPEND (FUNC_PREFIX, ecvt) (value, ndigit, decpt, sign)
 {
   static char buf[MAXDIG];
 
-  (void) ecvt_r (value, ndigit, decpt, sign, buf, sizeof buf);
+  (void) APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign,
+                                      buf, sizeof buf);
 
   return buf;
 }
index e6030c8543a8948428190db3568ee61a42e8dc36..7ad057fcb727f91ab2151e0fc6048ef4d654cfff 100644 (file)
@@ -37,6 +37,9 @@
 #define FLOOR APPEND(floor, FLOAT_NAME_EXT)
 #define FABS APPEND(fabs, FLOAT_NAME_EXT)
 #define LOG10 APPEND(log10, FLOAT_NAME_EXT)
+#define EXP APPEND(exp, FLOAT_NAME_EXT)
+#define ISINF APPEND(isinf, FLOAT_NAME_EXT)
+#define ISNAN APPEND(isnan, FLOAT_NAME_EXT)
 
 
 int
@@ -54,9 +57,15 @@ APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len)
       return -1;
     }
 
-  *sign = value < 0.0;
-  if (*sign)
-    value = - value;
+  if (!ISINF (value) && !ISNAN (value))
+    {
+      /* OK, the following is not entirely correct.  -0.0 is not handled
+        correctly but glibc 2.0 does not have a portable function to
+        implement this test.  */
+      *sign = value < 0.0;
+      if (*sign)
+       value = -value;
+    }
 
   n = snprintf (buf, len, "%.*" FLOAT_FMT_FLAG "f", ndigit, value);
   if (n < 0)
@@ -66,16 +75,29 @@ APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len)
   while (i < n && isdigit (buf[i]))
     ++i;
   *decpt = i;
-  do
-    ++i;
-  while (! isdigit (buf[i]));
-  memmove (&buf[i - *decpt], buf, n - (i - *decpt));
+
+  if (i == 0)
+    {
+      /* Value is Inf or NaN.  */
+      *sign = 0;
+      return 0;
+    }
+
+  if (i < n)
+    {
+      do
+       ++i;
+      while (i < n && !isdigit (buf[i]));
+      memmove (&buf[*decpt], &buf[i], n - i);
+      buf[n - (i - *decpt)] = 0;
+    }
 
   return 0;
 }
 
 #define weak_extern2(name) weak_extern (name)
 weak_extern2 (FLOOR) weak_extern2 (LOG10) weak_extern2 (FABS)
+weak_extern2 (EXP)
 
 int
 APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len)
@@ -84,24 +106,55 @@ APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len)
      char *buf;
      size_t len;
 {
-  FLOAT_TYPE (*log10_function) (FLOAT_TYPE) = &LOG10;
+  int exponent = 0;
 
-  if (log10_function)
-    {
-      /* Use the reasonable code if -lm is included.  */
-      ndigit -= (int) FLOOR (LOG10 (FABS (value)));
-      if (ndigit < 0)
-       ndigit = 0;
-    }
-  else
+  if (!ISNAN (value) && !ISINF (value) && value != 0.0)
     {
-      /* Slow code that doesn't require -lm functions.  */
-      FLOAT_TYPE d;
-      for (d = value < 0.0 ? - value : value;
-          ndigit > 0 && d >= 10.0;
-          d *= 0.1)
-       --ndigit;
+      FLOAT_TYPE (*log10_function) (FLOAT_TYPE) = &LOG10;
+
+      if (log10_function)
+       {
+         /* Use the reasonable code if -lm is included.  */
+         FLOAT_TYPE dexponent;
+         dexponent = FLOOR (LOG10 (FABS (value)));
+         value *= EXP (dexponent * -M_LN10);
+         exponent = (int) dexponent;
+       }
+      else
+       {
+         /* Slow code that doesn't require -lm functions.  */
+         FLOAT_TYPE d;
+         if (value < 0.0)
+           d = -value;
+         else
+           d = value;
+         if (d < 1.0)
+           {
+             do
+               {
+                 d *= 10.0;
+                 exponent--;
+               }
+             while (d < 1.0);
+           }
+         else if (d >= 10.0)
+           {
+             do
+               {
+                 d *= 0.1;
+                 exponent++;
+               }
+             while (d >= 10.0);
+           }
+         if (value < 0.0)
+           value = -d;
+         else
+           value = d;
+       }
     }
 
-  return APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len);
+  if (APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit - 1, decpt, sign, buf, len))
+    return -1;
+  *decpt += exponent;
+  return 0;
 }