summaryrefslogtreecommitdiffstats
path: root/generic
diff options
context:
space:
mode:
Diffstat (limited to 'generic')
-rwxr-xr-xgeneric/tclStrToD.c32
-rw-r--r--generic/tclUtil.c54
2 files changed, 77 insertions, 9 deletions
diff --git a/generic/tclStrToD.c b/generic/tclStrToD.c
index 51a1a58..53ffedd 100755
--- a/generic/tclStrToD.c
+++ b/generic/tclStrToD.c
@@ -12,7 +12,7 @@
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
- * RCS: @(#) $Id: tclStrToD.c,v 1.52 2010/12/07 16:27:39 dkf Exp $
+ * RCS: @(#) $Id: tclStrToD.c,v 1.53 2011/01/15 18:10:19 kennykb Exp $
*/
#include "tclInt.h"
@@ -3086,6 +3086,11 @@ StrictInt64Conversion(
if (i == ilim) {
if (2*b > S || (2*b == S && (digit & 1) != 0)) {
s = BumpUp(s, retval, &k);
+ } else {
+ while (*--s == '0') {
+ /* do nothing */
+ }
+ ++s;
}
break;
}
@@ -3330,6 +3335,11 @@ ShorteningBignumConversionPowD(
*s++ = '9';
s = BumpUp(s, retval, &k);
break;
+ } else if (mp_iszero(&b)) {
+ while (*--s == '0') {
+ /* do nothing */
+ }
+ ++s;
}
}
@@ -3494,6 +3504,10 @@ StrictBignumConversionPowD(
if (ShouldBankerRoundUpPowD(&b, sd, digit&1)) {
s = BumpUp(s, retval, &k);
}
+ while (*--s == '0') {
+ /* do nothing */
+ }
+ ++s;
break;
}
@@ -3766,7 +3780,7 @@ ShorteningBignumConversion(
--s5;
/*
- * TODO: It might possibly be a win to fall back to int64
+ * IDEA: It might possibly be a win to fall back to int64
* arithmetic here if S < 2**64/10. But it's a win only for
* a fairly narrow range of magnitudes so perhaps not worth
* bothering. We already know that we shorten the
@@ -3966,6 +3980,10 @@ StrictBignumConversion(
}
}
}
+ while (*--s == '0') {
+ /* do nothing */
+ }
+ ++s;
/*
* Endgame - store the location of the decimal point and the end of the
@@ -4023,9 +4041,10 @@ StrictBignumConversion(
* choosing the one that is closest to the given number (and
* resolving ties with 'round to even'). It is allowed to return
* fewer than 'ndigits' if the number converts exactly; if the
- * TCL_DD_E_FORMAT|TCL_DD_SHORTEN_FLAG is supplied instead, it is
- * also allowed to return fewer digits if the shorter string will
- * still reconvert to the given input number.
+ * TCL_DD_E_FORMAT|TCL_DD_SHORTEN_FLAG is supplied instead, it
+ * also returns fewer digits if the shorter string will still
+ * reconvert without loss to the given input number. In any case,
+ * strings of trailing zeroes are suppressed.
* TCL_DD_F_FORMAT - This value is used to prepare numbers for %f format
* conversion. It requests that conversion proceed until
* 'ndigits' digits after the decimal point have been converted.
@@ -4035,7 +4054,8 @@ StrictBignumConversion(
* number that converts exactly, and changing the argument to
* TCL_DD_F_FORMAT|TCL_DD_SHORTEN_FLAG will allow the routine
* also to return fewer digits if the shorter string will still
- * reconvert without loss to the given input number.
+ * reconvert without loss to the given input number. Strings of
+ * trailing zeroes are suppressed.
*
* To any of these flags may be OR'ed TCL_DD_NO_QUICK; this flag requires
* all calculations to be done in exact arithmetic. Normally, E and F
diff --git a/generic/tclUtil.c b/generic/tclUtil.c
index 984ba0a..f746aed 100644
--- a/generic/tclUtil.c
+++ b/generic/tclUtil.c
@@ -11,7 +11,7 @@
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
- * RCS: @(#) $Id: tclUtil.c,v 1.123 2010/12/03 22:30:56 hobbs Exp $
+ * RCS: @(#) $Id: tclUtil.c,v 1.124 2011/01/15 18:10:19 kennykb Exp $
*/
#include "tclInt.h"
@@ -2273,7 +2273,47 @@ Tcl_PrintDouble(
digits = TclDoubleDigits(value, -1, TCL_DD_SHORTEST,
&exponent, &signum, &end);
} else {
- digits = TclDoubleDigits(value, *precisionPtr, TCL_DD_E_FORMAT,
+ /*
+ * There are at least two possible interpretations for tcl_precision.
+ *
+ * The first is, "choose the decimal representation having
+ * $tcl_precision digits of significance that is nearest to the
+ * given number, breaking ties by rounding to even, and then
+ * trimming trailing zeros." This gives the greatest possible
+ * precision in the decimal string, but offers the anomaly that
+ * [expr 0.1] will be "0.10000000000000001".
+ *
+ * The second is "choose the decimal representation having at
+ * most $tcl_precision digits of significance that is nearest
+ * to the given number. If no such representation converts
+ * exactly to the given number, choose the one that is closest,
+ * breaking ties by rounding to even. If more than one such
+ * representation converts exactly to the given number, choose
+ * the shortest, breaking ties in favour of the nearest, breaking
+ * remaining ties in favour of the one ending in an even digit."
+ *
+ * Tcl 8.4 implements the first of these, which gives rise to
+ * anomalies in formatting:
+ *
+ * % expr 0.1
+ * 0.10000000000000001
+ * % expr 0.01
+ * 0.01
+ * % expr 1e-7
+ * 9.9999999999999995e-08
+ *
+ * For human readability, it appears better to choose the second rule,
+ * and let [expr 0.1] return 0.1. But for 8.4 compatibility, we
+ * prefer the first (the recommended zero value for tcl_precision
+ * avoids the problem entirely).
+ *
+ * Uncomment TCL_DD_SHORTEN_FLAG in the next call to prefer the
+ * method that allows floating point values to be shortened if
+ * it can be done without loss of precision.
+ */
+
+ digits = TclDoubleDigits(value, *precisionPtr,
+ TCL_DD_E_FORMAT /* | TCL_DD_SHORTEN_FLAG */,
&exponent, &signum, &end);
}
if (signum) {
@@ -2294,7 +2334,15 @@ Tcl_PrintDouble(
c = *++p;
}
}
- sprintf(dst, "e%+d", exponent);
+ /*
+ * Tcl 8.4 appears to format with at least a two-digit exponent;
+ * preserve that behaviour when tcl_precision != 0
+ */
+ if (*precisionPtr == 0) {
+ sprintf(dst, "e%+d", exponent);
+ } else {
+ sprintf(dst, "e%+03d", exponent);
+ }
} else {
/*
* F format for others.