about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Stephenson <pws@users.sourceforge.net>2007-11-06 11:04:34 +0000
committerPeter Stephenson <pws@users.sourceforge.net>2007-11-06 11:04:34 +0000
commitb9f3fc7c81dfde1400ebec848a1356116d2e95aa (patch)
treef82a8d777ac11400fb9f4e6fde04e7125c8fa811
parent154ebe3e9668f65909facaef9cb64c9c4697903b (diff)
downloadzsh-b9f3fc7c81dfde1400ebec848a1356116d2e95aa.tar.gz
zsh-b9f3fc7c81dfde1400ebec848a1356116d2e95aa.tar.xz
zsh-b9f3fc7c81dfde1400ebec848a1356116d2e95aa.zip
24068: attempt to make `printf "%g\n" -0 output "-0"
-rw-r--r--ChangeLog6
-rw-r--r--Src/builtin.c22
2 files changed, 25 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index 8fcd16524..2fd6cb9a2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2007-11-06  Peter Stephenson  <pws@csr.com>
+
+	* 24068: Src/builtin.c: attempt to make `printf "%g\n" -0'
+	output "-0", although this depends on the vagaries of the
+	library's strtod() (works on Solaris, doesn't on Fedora 7).
+
 2007-11-04  Wayne Davison  <wayned@users.sourceforge.net>
 
 	* unposted: Completion/Unix/Command/_rsync:  Added new options
diff --git a/Src/builtin.c b/Src/builtin.c
index 7420ed887..d69ab5591 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -4162,9 +4162,25 @@ bin_print(char *name, char **args, Options ops, int func)
 			    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;