From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 30317 invoked by alias); 18 Dec 2019 16:49:15 -0000 Mailing-List: contact newlib-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: newlib-owner@sourceware.org Received: (qmail 30283 invoked by uid 89); 18 Dec 2019 16:49:15 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.7 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy=HContent-Transfer-Encoding:8bit X-HELO: elaine.keithp.com Received: from home.keithp.com (HELO elaine.keithp.com) (63.227.221.253) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 18 Dec 2019 16:49:14 +0000 Received: from localhost (localhost [127.0.0.1]) by elaine.keithp.com (Postfix) with ESMTP id B3C823F2A0A4 for ; Wed, 18 Dec 2019 08:49:12 -0800 (PST) Received: from elaine.keithp.com ([127.0.0.1]) by localhost (elaine.keithp.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id NflcirVRTceH; Wed, 18 Dec 2019 08:49:12 -0800 (PST) Received: from keithp.com (koto.keithp.com [10.0.0.2]) by elaine.keithp.com (Postfix) with ESMTPSA id 61BAE3F2A0A0; Wed, 18 Dec 2019 08:49:12 -0800 (PST) Received: by keithp.com (Postfix, from userid 1000) id 4750415821F0; Wed, 18 Dec 2019 08:49:12 -0800 (PST) From: Keith Packard To: newlib@sourceware.org Cc: Keith Packard Subject: [PATCH 2/2] Fix gcvt to always show 'ndigits' of precision Date: Wed, 18 Dec 2019 16:49:00 -0000 Message-Id: <20191218164906.1124916-2-keithp@keithp.com> In-Reply-To: <20191218164906.1124916-1-keithp@keithp.com> References: <20191218164906.1124916-1-keithp@keithp.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-IsSubscribed: yes X-SW-Source: 2019/txt/msg00678.txt.bz2 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 --- 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