From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27842 invoked by alias); 30 Apr 2007 20:34:28 -0000 Received: (qmail 27825 invoked by uid 22791); 30 Apr 2007 20:34:28 -0000 X-Spam-Check-By: sourceware.org Received: from sunsite.ms.mff.cuni.cz (HELO sunsite.mff.cuni.cz) (195.113.15.26) by sourceware.org (qpsmtpd/0.31) with ESMTP; Mon, 30 Apr 2007 21:34:25 +0100 Received: from sunsite.mff.cuni.cz (localhost.localdomain [127.0.0.1]) by sunsite.mff.cuni.cz (8.13.8/8.13.8) with ESMTP id l3UKgAPl008519; Mon, 30 Apr 2007 22:42:10 +0200 Received: (from jakub@localhost) by sunsite.mff.cuni.cz (8.13.8/8.13.8/Submit) id l3UKgAEv008515; Mon, 30 Apr 2007 22:42:10 +0200 Date: Mon, 30 Apr 2007 20:34:00 -0000 From: Jakub Jelinek To: Ulrich Drepper Cc: Glibc hackers Subject: [PATCH] Fix printf %e .9999999999999999 Message-ID: <20070430204209.GR1826@sunsite.mff.cuni.cz> Reply-To: Jakub Jelinek Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.4.2.2i Mailing-List: contact libc-hacker-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-hacker-owner@sourceware.org X-SW-Source: 2007-04/txt/msg00013.txt.bz2 Hi! We print ATM 0.999999999 with "%e" as "1.000000e-00", while 1.0 as "1.000000e+00". While the standard doesn't talk about the sign of the exponent when it is zero, as IEEE754 numbers don't have positive vs. negative zero exponents, just exponent 0, I'd say we should always print e+00 instead of e-00. 2007-04-30 Jakub Jelinek * stdio-common/printf_fp.c (___printf_fp): Don't print negative sign for exponent 0. * stdio-common/tfformat.c (sprint_doubles): Add a new test. --- libc/stdio-common/printf_fp.c.jj 2007-04-23 10:54:03.000000000 +0200 +++ libc/stdio-common/printf_fp.c 2007-04-30 22:06:04.000000000 +0200 @@ -793,7 +793,7 @@ ___printf_fp (FILE *fp, else { /* This is a special case. We don't need a factor because the - numbers are in the range of 0.0 <= fp < 8.0. We simply + numbers are in the range of 1.0 <= |fp| < 8.0. We simply shift it to the right place and divide it by 1.0 to get the leading digit. (Of course this division is not really made.) */ assert (0 <= exponent && exponent < 3 && @@ -1013,6 +1013,12 @@ ___printf_fp (FILE *fp, { *wstartp = '1'; exponent += expsign == 0 ? 1 : -1; + + /* The above exponent adjustment could lead to 1.0e-00, + e.g. for 0.999999999. Make sure exponent 0 always + uses + sign. */ + if (exponent == 0) + expsign = 0; } else if (intdig_no == dig_max) { --- libc/stdio-common/tfformat.c.jj 2007-04-23 10:54:04.000000000 +0200 +++ libc/stdio-common/tfformat.c 2007-04-30 22:21:42.000000000 +0200 @@ -4021,6 +4021,8 @@ sprint_double_type sprint_doubles[] = {__LINE__, 0.000098, "0.0001", "%#.0g"}, {__LINE__, 0.0000996, "0.00010", "%#.2g"}, {__LINE__, 9.999999999999999e-05, "0.0001", "%g"}, + {__LINE__, 1.0, "1.000000e+00", "%e"}, + {__LINE__, .9999999999999999, "1.000000e+00", "%e"}, {0 } Jakub