public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Keith Packard <keithp@keithp.com>
To: newlib@sourceware.org
Cc: Keith Packard <keithp@keithp.com>
Subject: [PATCH 2/2] Fix gcvt to always show 'ndigits' of precision
Date: Wed, 18 Dec 2019 16:49:00 -0000	[thread overview]
Message-ID: <20191218164906.1124916-2-keithp@keithp.com> (raw)
In-Reply-To: <20191218164906.1124916-1-keithp@keithp.com>

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 <keithp@keithp.com>
---
 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

      reply	other threads:[~2019-12-18 16:49 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-18 16:49 [PATCH 1/2] Fix fcvt to only show 'ndigit' past decimal Keith Packard
2019-12-18 16:49 ` Keith Packard [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191218164906.1124916-2-keithp@keithp.com \
    --to=keithp@keithp.com \
    --cc=newlib@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).