summaryrefslogtreecommitdiffstats
path: root/generic/tclArithSeries.c
diff options
context:
space:
mode:
authorgriffin <briang42@easystreet.net>2023-06-30 14:34:09 (GMT)
committergriffin <briang42@easystreet.net>2023-06-30 14:34:09 (GMT)
commiteb1f9c99892df6f4227bfedc9ddd3ade75edcc4e (patch)
treedab4b59fd48a4f8fc04057e2901fe1a6603afb54 /generic/tclArithSeries.c
parentbd647296865ed931db4903444b182da472f91cae (diff)
parentcd82527608d6856590780a5c0cf5b6fde1dfbadf (diff)
downloadtcl-eb1f9c99892df6f4227bfedc9ddd3ade75edcc4e.zip
tcl-eb1f9c99892df6f4227bfedc9ddd3ade75edcc4e.tar.gz
tcl-eb1f9c99892df6f4227bfedc9ddd3ade75edcc4e.tar.bz2
merge trunk
Diffstat (limited to 'generic/tclArithSeries.c')
-rwxr-xr-xgeneric/tclArithSeries.c27
1 files changed, 18 insertions, 9 deletions
diff --git a/generic/tclArithSeries.c b/generic/tclArithSeries.c
index caa69a0..caf701b 100755
--- a/generic/tclArithSeries.c
+++ b/generic/tclArithSeries.c
@@ -152,6 +152,9 @@ ArithSeriesGetInternalRep(Tcl_Obj *objPtr)
return irPtr ? (ArithSeries *)irPtr->twoPtrValue.ptr1 : NULL;
}
+/*
+ * Compute number of significant factional digits
+ */
static inline int
Precision(double d)
{
@@ -161,10 +164,13 @@ Precision(double d)
off = strchr(tmp, '.');
return (off ? strlen(off+1) : 0);
}
+
+/*
+ * Find longest number of digits after the decimal point.
+ */
static inline int
maxPrecision(double start, double end, double step)
{
- // Find longest number of digits after the decimal point.
int dp = Precision(step);
int i = Precision(start);
dp = i>dp ? i : dp;
@@ -211,15 +217,17 @@ ArithSeriesLenInt(Tcl_WideInt start, Tcl_WideInt end, Tcl_WideInt step)
}
static Tcl_WideInt
-ArithSeriesLenDbl(double start, double end, double step)
+ArithSeriesLenDbl(double start, double end, double step, int precision)
{
- Tcl_WideInt len;
-
+ double istart, iend, istep, ilen;
if (step == 0) {
return 0;
}
- len = ((end-start+step)/step);
- return (len < 0) ? -1 : len;
+ istart = start * pow(10,precision);
+ iend = end * pow(10,precision);
+ istep = step * pow(10,precision);
+ ilen = ((iend-istart+istep)/istep);
+ return floor(ilen);
}
@@ -533,7 +541,8 @@ TclNewArithSeriesObj(
assert(dstep!=0);
if (!lenObj) {
if (useDoubles) {
- len = ArithSeriesLenDbl(dstart, dend, dstep);
+ int precision = maxPrecision(dstart,dend,dstep);
+ len = ArithSeriesLenDbl(dstart, dend, dstep, precision);
} else {
len = ArithSeriesLenInt(start, end, step);
}
@@ -668,7 +677,6 @@ TclArithSeriesObjStep(
return TCL_OK;
}
-
/*
*----------------------------------------------------------------------
*
@@ -798,7 +806,8 @@ TclArithSeriesObjRange(
arithSeriesDblRepPtr->end = end;
arithSeriesDblRepPtr->step = step;
arithSeriesDblRepPtr->precision = maxPrecision(start, end, step);
- arithSeriesDblRepPtr->len = ArithSeriesLenDbl(start, end, step);
+ arithSeriesDblRepPtr->len =
+ ArithSeriesLenDbl(start, end, step, arithSeriesDblRepPtr->precision);
arithSeriesDblRepPtr->elements = NULL;
} else {