From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 125688 invoked by alias); 18 Dec 2019 19:56:58 -0000 Mailing-List: contact newlib-cvs-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: newlib-cvs-owner@sourceware.org Received: (qmail 125670 invoked by uid 9078); 18 Dec 2019 19:56:58 -0000 Date: Wed, 18 Dec 2019 19:56:00 -0000 Message-ID: <20191218195658.125667.qmail@sourceware.org> Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Corinna Vinschen To: newlib-cvs@sourceware.org Subject: [newlib-cygwin] Fix gcvt to always show 'ndigits' of precision X-Act-Checkin: newlib-cygwin X-Git-Author: Keith Packard X-Git-Refname: refs/heads/master X-Git-Oldrev: abcc586ffec05a7779f8474119407d050392ee18 X-Git-Newrev: 11f99384d2971356bab2fcac7e29792250abea73 X-SW-Source: 2019-q4/txt/msg00029.txt.bz2 https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=11f99384d2971356bab2fcac7e29792250abea73 commit 11f99384d2971356bab2fcac7e29792250abea73 Author: Keith Packard 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 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 */