From b9f3fc7c81dfde1400ebec848a1356116d2e95aa Mon Sep 17 00:00:00 2001 From: Peter Stephenson Date: Tue, 6 Nov 2007 11:04:34 +0000 Subject: 24068: attempt to make `printf "%g\n" -0 output "-0" --- Src/builtin.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'Src/builtin.c') 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; -- cgit 1.4.1