public inbox for newlib-cvs@sourceware.org
help / color / mirror / Atom feed
* [newlib-cygwin] Fix gcvt to always show 'ndigits' of precision
@ 2019-12-18 19:56 Corinna Vinschen
  0 siblings, 0 replies; only message in thread
From: Corinna Vinschen @ 2019-12-18 19:56 UTC (permalink / raw)
  To: newlib-cvs

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=11f99384d2971356bab2fcac7e29792250abea73

commit 11f99384d2971356bab2fcac7e29792250abea73
Author: Keith Packard <keithp@keithp.com>
Date:   Wed Dec 18 08:49:06 2019 -0800

    Fix gcvt to always show 'ndigits' of precision
    
    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>

Diff:
---
 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 12e8c9a..228362e 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 */


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-12-18 19:56 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-18 19:56 [newlib-cygwin] Fix gcvt to always show 'ndigits' of precision Corinna Vinschen

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).