diff options
Diffstat (limited to 'generic/tclArithSeries.c')
| -rwxr-xr-x | generic/tclArithSeries.c | 27 |
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 { |
