[PATCH 1/2] Fix fcvt to only show 'ndigit' past decimal

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH 1/2] Fix fcvt to only show 'ndigit' past decimal

Keith Packard
Even if the number is really small and this means showing *no* digits.
This makes newlib match glibc, and the fcvt posix man page.

Signed-off-by: Keith Packard <[hidden email]>
---
 newlib/libc/stdlib/ecvtbuf.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)

diff --git a/newlib/libc/stdlib/ecvtbuf.c b/newlib/libc/stdlib/ecvtbuf.c
index e3d7b55d8..12e8c9a92 100644
--- a/newlib/libc/stdlib/ecvtbuf.c
+++ b/newlib/libc/stdlib/ecvtbuf.c
@@ -235,14 +235,7 @@ fcvtbuf (double invalue,
 
   save = fcvt_buf;
 
-  if (invalue < 1.0 && invalue > -1.0)
-    {
-      p = _dtoa_r (reent, invalue, 2, ndigit, decpt, sign, &end);
-    }
-  else
-    {
-      p = _dtoa_r (reent, invalue, 3, ndigit, decpt, sign, &end);
-    }
+  p = _dtoa_r (reent, invalue, 3, ndigit, decpt, sign, &end);
 
   /* Now copy */
 
--
2.24.0

Reply | Threaded
Open this post in threaded view
|

[PATCH 2/2] Fix gcvt to always show 'ndigits' of precision

Keith Packard
Leading zeros after the decimal point should not count
towards the 'ndigits' limit.

This makes gcvt match glibc and the posix gcvt man page.

Signed-off-by: Keith Packard <[hidden email]>
---
 newlib/libc/stdlib/ecvtbuf.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/newlib/libc/stdlib/ecvtbuf.c b/newlib/libc/stdlib/ecvtbuf.c
index 12e8c9a92..228362e07 100644
--- a/newlib/libc/stdlib/ecvtbuf.c
+++ b/newlib/libc/stdlib/ecvtbuf.c
@@ -349,15 +349,10 @@ _gcvt (struct _reent *ptr,
       char *end;
       char *p;
 
-      if (invalue < 1.0)
- {
-  /* what we want is ndigits after the point */
-  p = _dtoa_r (ptr, invalue, 3, ndigit, &decpt, &sign, &end);
- }
-      else
- {
-  p = _dtoa_r (ptr, invalue, 2, ndigit, &decpt, &sign, &end);
- }
+      /* We always want ndigits of precision, even if that means printing
+       * a bunch of leading zeros for numbers < 1.0
+       */
+      p = _dtoa_r (ptr, invalue, 2, ndigit, &decpt, &sign, &end);
 
       if (decpt == 9999)
  {
@@ -383,11 +378,12 @@ _gcvt (struct _reent *ptr,
   if (buf == save)
     *buf++ = '0';
   *buf++ = '.';
-  while (decpt < 0 && ndigit > 0)
+
+  /* Leading zeros don't count towards 'ndigit' */
+  while (decpt < 0)
     {
       *buf++ = '0';
       decpt++;
-      ndigit--;
     }
 
   /* Print rest of stuff */
--
2.24.0