From nobody@FreeBSD.org  Tue Aug 20 10:32:56 2002
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 7D45937B400
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 20 Aug 2002 10:32:56 -0700 (PDT)
Received: from www.freebsd.org (www.FreeBSD.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 3F70143E42
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 20 Aug 2002 10:32:56 -0700 (PDT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.12.4/8.12.4) with ESMTP id g7KHWtOT034153
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 20 Aug 2002 10:32:55 -0700 (PDT)
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.12.4/8.12.4/Submit) id g7KHWtKV034152;
	Tue, 20 Aug 2002 10:32:55 -0700 (PDT)
Message-Id: <200208201732.g7KHWtKV034152@www.freebsd.org>
Date: Tue, 20 Aug 2002 10:32:55 -0700 (PDT)
From: GOTO Kentaro <gotoken@notwork.org>
To: freebsd-gnats-submit@FreeBSD.org
Subject: printf("%+f\n", -0.0) generates +0.000000
X-Send-Pr-Version: www-1.0

>Number:         41823
>Category:       bin
>Synopsis:       printf("%+f\n", -0.0) generates +0.000000
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Aug 20 10:40:28 PDT 2002
>Closed-Date:    Fri Jan 31 12:18:31 PST 2003
>Last-Modified:  Fri Jan 31 12:18:31 PST 2003
>Originator:     GOTO Kentaro
>Release:        STABLE and CURRENT
>Organization:
>Environment:
FreeBSD 5.0-CURRENT i386

>Description:
vfprintf() generates a wrong "+" sign for a floating point number 
minus zero, i.e., -0.0, when +[eEfgG] is specified in the format.  

>How-To-Repeat:
% echo 'main(){printf("%+f\\n", -0.0);}' | /usr/bin/cc -xc - && ./a.out
+0.000000
      

>Fix:
How about Checking sign bit instead of `< 0'. 

For example, NetBSD's /usr/src/lib/libc/stdio/vfprintf.c:cvt() does 

        digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve);
        if (dsgn) {
                value = -value;
                *sign = '-';
        } else
                *sign = '\000';

But FreeBSD's does

        if (value < 0) {
                value = -value;
                *sign = '-';
        } else
                *sign = '\000';

This test does not work in this case because IEEE754 defines the 
truth value of -ZERO < ZERO as FALSE. 

>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->analyzed 
State-Changed-By: schweikh 
State-Changed-When: Wed Aug 21 04:10:56 PDT 2002 
State-Changed-Why:  
You are correct, ISO9899:1999 states in footnote 233) 
"The results of all floating conversions of a negative zero, 
and of negative values that round to zero, include a minus sign." 

Can you try this patch against -current? (stable should be easy too). 
This is what you suggested, I believe, i.e. moving the __dtoa() 
before the comparison and using "if (dsgn)" instead of value<0. 
This would align us a little closer to NetBSD as you stated. 

Index: src/lib/libc/stdio/vfprintf.c 
=================================================================== 
RCS file: /home/ncvs/src/lib/libc/stdio/vfprintf.c,v 
retrieving revision 1.43 
diff -u -r1.43 vfprintf.c 
--- src/lib/libc/stdio/vfprintf.c	15 Aug 2002 10:28:52 -0000	1.43 
+++ src/lib/libc/stdio/vfprintf.c	21 Aug 2002 11:17:14 -0000 
@@ -1409,13 +1409,13 @@ 
ndigits++; 
mode = 2;		/* ndigits significant digits */ 
} 
-	if (value < 0) { 
+	digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve, 
+			dtoaresultp); 
+	if (dsgn) { 
value = -value; 
*sign = '-'; 
} else 
*sign = '000'; 
-	digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve, 
-			dtoaresultp); 
if ((ch != 'g' && ch != 'G') || flags & ALT) { 
/* print trailing zeros */ 
bp = digits + ndigits; 

http://www.freebsd.org/cgi/query-pr.cgi?pr=41823 

From: Jens Schweikhardt <schweikh@schweikhardt.net>
To: Bruce Evans <bde@zeta.org.au>
Cc: GNATS Bug Followup <bug-followup@FreeBSD.org>
Subject: Re: bin/41823: printf("%+f\n", -0.0) generates +0.000000
Date: Sun, 25 Aug 2002 15:31:21 +0200

 On Thu, Aug 22, 2002 at 09:36:30AM +1000, Bruce Evans wrote:
 ...
 # % ===================================================================
 # % RCS file: /home/ncvs/src/lib/libc/stdio/vfprintf.c,v
 # % retrieving revision 1.43
 # % diff -u -r1.43 vfprintf.c
 # % --- src/lib/libc/stdio/vfprintf.c	15 Aug 2002 10:28:52 -0000	1.43
 # % +++ src/lib/libc/stdio/vfprintf.c	21 Aug 2002 11:17:14 -0000
 # % @@ -1409,13 +1409,13 @@
 # %  			ndigits++;
 # %  		mode = 2;		/* ndigits significant digits */
 # %  	}
 # % -	if (value < 0) {
 # % +	digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve,
 # % +			dtoaresultp);
 # % +	if (dsgn) {
 # %  		value = -value;
 # %  		*sign = '-';
 # %  	} else
 # %  		*sign = '\000';
 # % -	digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve,
 # % -			dtoaresultp);
 # %  	if ((ch != 'g' && ch != 'G') || flags & ALT) {
 # %  		/* print trailing zeros */
 # %  		bp = digits + ndigits;
 # %
 # 
 # Style bugs:
 # new:
 #     negating "value" after using it is bogus.  (It is used later, but only
 #     as a boolean.)
 # old:
 #     don't need to split the __dtoa() line.
 # very old (in rev.1.1):
 #     *sign is a really a boolean, so setting to character values is bogus.
 #     (It is named *softsign in the only caller and used to set a variable
 #     named "sign" which really holds a character value there.  The caller
 #     also spells '\0' normally.  Actually that '\0' is really a boolean
 #     too -- it never gets printed for obvious reasons.)
 
 Ok, here's a patch you'll like -- it reduces the lines of code :-)
 
 Index: vfprintf.c
 ===================================================================
 RCS file: /home/ncvs/src/lib/libc/stdio/vfprintf.c,v
 retrieving revision 1.43
 diff -u -r1.43 vfprintf.c
 --- vfprintf.c	15 Aug 2002 10:28:52 -0000	1.43
 +++ vfprintf.c	25 Aug 2002 13:26:08 -0000
 @@ -1409,13 +1409,8 @@
  			ndigits++;
  		mode = 2;		/* ndigits significant digits */
  	}
 -	if (value < 0) {
 -		value = -value;
 -		*sign = '-';
 -	} else
 -		*sign = '\000';
 -	digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve,
 -			dtoaresultp);
 +	digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve, dtoaresultp);
 +	*sign = dsgn != 0;
  	if ((ch != 'g' && ch != 'G') || flags & ALT) {
  		/* print trailing zeros */
  		bp = digits + ndigits;
 
 Regards,
 
 	Jens
 -- 
 Jens Schweikhardt http://www.schweikhardt.net/
 SIGSIG -- signature too long (core dumped)

From: Bruce Evans <bde@zeta.org.au>
To: Jens Schweikhardt <schweikh@schweikhardt.net>
Cc: GNATS Bug Followup <bug-followup@FreeBSD.org>
Subject: Re: bin/41823: printf("%+f\n", -0.0) generates +0.000000
Date: Mon, 26 Aug 2002 11:25:06 +1000 (EST)

 On Sun, 25 Aug 2002, Jens Schweikhardt wrote:
 
 > Ok, here's a patch you'll like -- it reduces the lines of code :-)
 >
 > Index: vfprintf.c
 > ===================================================================
 > RCS file: /home/ncvs/src/lib/libc/stdio/vfprintf.c,v
 > retrieving revision 1.43
 > diff -u -r1.43 vfprintf.c
 > --- vfprintf.c	15 Aug 2002 10:28:52 -0000	1.43
 > +++ vfprintf.c	25 Aug 2002 13:26:08 -0000
 > @@ -1409,13 +1409,8 @@
 >  			ndigits++;
 >  		mode = 2;		/* ndigits significant digits */
 >  	}
 > -	if (value < 0) {
 > -		value = -value;
 > -		*sign = '-';
 > -	} else
 > -		*sign = '\000';
 > -	digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve,
 > -			dtoaresultp);
 > +	digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve, dtoaresultp);
 > +	*sign = dsgn != 0;
 >  	if ((ch != 'g' && ch != 'G') || flags & ALT) {
 >  		/* print trailing zeros */
 >  		bp = digits + ndigits;
 >
 
 Yes, I like it.  Assuming that it works of course.
 
 Bruce
 
State-Changed-From-To: analyzed->patched 
State-Changed-By: schweikh 
State-Changed-When: Tue Aug 27 13:11:19 PDT 2002 
State-Changed-Why:  
Committed to -current. MFC in 3 weeks, if I get re@ approval, 
else after 4.7R is out the door. Thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=41823 
State-Changed-From-To: patched->closed 
State-Changed-By: schweikh 
State-Changed-When: Fri Jan 31 12:16:33 PST 2003 
State-Changed-Why:  
MFC'd back in October. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=41823 
>Unformatted:
