Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Bug#448732: zsh: printf %g -0 outputs 0 instead of -0



On Wed, 31 Oct 2007 12:38:38 -0400
Clint Adams <schizo@xxxxxxxxxx> wrote:
> I believe the gist is that since %g calls for a float, -0 should be
> parsed as negative zero.

I tried the following patch, but on my system (Fedora 7 with glibc
glibc-2.6-4) strtod() returns the double value 0 (not -0) when parsing
the input "-0".  I confirmed this with a standalone programme.  It seems
pointless working around this in the shell.

===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.180
diff -u -r1.180 builtin.c
--- Src/builtin.c	6 Jul 2007 21:52:39 -0000	1.180
+++ Src/builtin.c	6 Nov 2007 10:39:29 -0000
@@ -4162,9 +4162,25 @@
 			    break;
 		    case 2:
 			if (curarg) {
-			    mnumval = matheval(curarg);
-			    doubleval = (mnumval.type & MN_FLOAT) ?
-			    	mnumval.u.d : (double)mnumval.u.l;
+			    char *eptr;
+			    /*
+			     * First attempt to parse as a floating
+			     * point constant.  If we go through
+			     * a math evaluation, we can lose
+			     * mostly unimportant information
+			     * that people in standards organizations
+			     * worry about.
+			     */
+			    doubleval = strtod(curarg, &eptr);
+			    /*
+			     * If it didn't parse as a constant,
+			     * parse it as an expression.
+			     */
+			    if (*eptr != '\0') {
+				mnumval = matheval(curarg);
+				doubleval = (mnumval.type & MN_FLOAT) ?
+				    mnumval.u.d : (double)mnumval.u.l;
+			    }
 			} else doubleval = 0;
 			if (errflag) {
 			    doubleval = 0;


-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070



Messages sorted by: Reverse Date, Date, Thread, Author