Lines Matching +full:k +full:- +full:to +full:- +full:j
8 Permission to use, copy, modify, and distribute this software and
14 not be used in advertising or publicity pertaining to
18 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
29 /* Please send bug reports to David M. Gay (dmg at acm dot org,
30 * with " at " changed at "@" and " dot " changed to "."). */
41 int i, k; local
46 k = 0;
49 k++;
52 if (!k)
53 k = 1;
55 b = Balloc(k);
56 be = bits + ((nbits - 1) >> kshift);
57 x = x0 = b->x;
64 i = x - x0;
65 while(!x0[--i])
67 b->wds = 0;
71 b->wds = i + 1;
72 *bbits = i*ULbits + 32 - hi0bits(b->x[i]);
77 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
79 * Inspired by "How to Print Floating-Point Numbers Accurately" by
80 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
84 * to determine k = floor(log10(d)). We scale relevant
85 * quantities using O(log2(k)) rather than O(k) multiplications.
86 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
87 * try to generate digits strictly left to right. Instead, we
93 * round-nearest rule will give the same floating-point value
98 * 5. When converting floating-point integers less than 1e16,
99 * we use floating-point arithmetic rather than resorting
100 * to multiple-precision integers.
101 * 6. When asked to produce fewer than 15 digits, we first try
102 * to get by with floating-point arithmetic; we resort to
103 * multiple-precision integer arithmetic only if we cannot
104 * guarantee that the floating-point calculation has given
105 * the correctly rounded result. For k requested digits and
107 * something like 10^(k-15) that we must resort to the Long
121 /* Arguments ndigits and decpt are similar to the second and third
123 the returned string. If not null, *rve is set to point
124 to the end of the return value. If d is +-Infinity or NaN,
125 then *decpt is set to 9999.
126 be = exponent: value = (integer represented by bits) * (2 to the power of be).
130 and rounded to nearest.
135 return value similar to that of ecvt, except
138 gives a return value similar to that from fcvt,
141 4-9 should give the same return values as 2-3, i.e.,
145 faster than modes 2-3.
146 4,5,8,9 ==> left-to-right digit generation.
147 6-9 ==> don't try fast floating-point estimate
150 Values of mode other than 0-9 are treated as mode 0.
152 Sufficient space is allocated to the return value
153 to hold the suppressed trailing zeros.
157 int j, j1, k, k0, k_check, kind, leftright, m2, m5, nbits; local
180 *decpt = -32768;
183 *decpt = -32768;
188 b = bitstob(bits, nbits = fpi->nbits, &bbits);
193 bbits -= i;
195 if (!b->wds) {
203 i = be + bbits - 1;
207 if ( (j = 11 - hi0bits(word0(&d) & Frac_mask)) !=0)
208 dval(&d) /= 1 << j;
211 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
213 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
214 * log10(&d) = (i-Bias)*log(2)/log(10) + log10(d2)
216 * This suggests computing an approximation k to log10(&d) by
218 * k = (i - Bias)*0.301029995663981
219 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
221 * We want k to be too large rather than too small.
222 * The error in the first-order Taylor series approximation
224 * to compensate for any error in the multiplication of
225 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
226 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
227 * adding 1e-13 to the constant term more than suffices.
228 * Hence we adjust the constant term to 0.1760912590558.
229 * (We could get a more accurate k by invoking log10,
234 i += j;
236 ds = (dval(&d)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
239 if ((j = i) < 0)
240 j = -j;
241 if ((j -= 1077) > 0)
242 ds += j * 7e-17;
244 k = (int)ds;
245 if (ds < 0. && ds != k)
246 k--; /* want k = floor(ds) */
249 j = be + bbits - 1;
250 if ( (j1 = j & 3) !=0)
252 word0(&d) += j << Exp_shift - 2 & Exp_mask;
254 word0(&d) += (be + bbits - 1) << Exp_shift;
256 if (k >= 0 && k <= Ten_pmax) {
257 if (dval(&d) < tens[k])
258 k--;
261 j = bbits - i - 1;
262 if (j >= 0) {
264 s2 = j;
267 b2 = -j;
270 if (k >= 0) {
272 s5 = k;
273 s2 += k;
276 b2 -= k;
277 b5 = -k;
284 mode -= 4;
287 else if (i >= -4 - Emin || i < Emin)
290 ilim = ilim1 = -1; /* Values for cases 0 and 1; done here to */
291 /* silence erroneous "gcc -Wall" warning. */
310 i = ndigits + k + 1;
312 ilim1 = i - 1;
318 if ( (rdir = fpi->rounding - 1) !=0) {
322 rdir = 3 - rdir;
329 && k == 0
333 /* Try to get by with floating-point arithmetic. */
338 if ( (j = 11 - hi0bits(word0(&d) & Frac_mask)) !=0)
339 dval(&d) /= 1 << j;
341 k0 = k;
344 if (k > 0) {
345 ds = tens[k&0xf];
346 j = k >> 4;
347 if (j & Bletch) {
349 j &= Bletch - 1;
350 dval(&d) /= bigtens[n_bigtens-1];
353 for(; j; j >>= 1, i++)
354 if (j & 1) {
361 if ( (j1 = -k) !=0) {
363 for(j = j1 >> 4; j; j >>= 1, i++)
364 if (j & 1) {
374 k--;
379 word0(&eps) -= (P-1)*Exp_msk1;
382 dval(&d) -= 5.;
385 if (dval(&d) < -dval(&eps))
394 dval(&eps) = ds*0.5/tens[ilim-1] - dval(&eps);
397 dval(&d) -= L*ds;
404 if (ds - dval(&d) < dval(&eps))
415 dval(&eps) *= tens[ilim-1];
418 dval(&d) -= L*ds;
424 else if (dval(&d) < ds - dval(&eps)) {
438 k = k0;
444 if (be >= 0 && k <= Int_max) {
446 ds = tens[k];
455 dval(&d) -= L*ds;
459 L--;
482 while(*--s == '9')
484 k++;
493 while(*--s == '0'){}
506 i = nbits - bbits;
507 if (be - i++ < fpi->emin && mode != 3 && mode != 5) {
509 i = be - fpi->emin + 1;
515 j = ilim - 1;
516 if (m5 >= j)
517 m5 -= j;
519 s5 += j -= m5;
520 b5 += j;
524 m2 -= i;
534 b2 -= i;
535 m2 -= i;
536 s2 -= i;
546 if ( (j = b5 - m5) !=0)
547 b = pow5mult(b, j);
560 if (bbits == 1 && be0 > fpi->emin + 1) {
572 * and for all and pass them and a shift to quorem, so it
573 * can do shifts and ors to compute the numerator for q.
575 i = ((s5 ? hi0bits(S->x[S->wds-1]) : ULbits - 1) - s2 - 4) & kmask;
583 k--;
584 b = multadd(b, 10, 0); /* we botched the k estimate */
594 k = -1 - ndigits;
601 k++;
608 /* Compute mlo -- check for special case
614 mhi = Balloc(mhi->k);
622 * that will round to d?
624 j = cmp(b, mlo);
626 j1 = delta->sign ? 1 : cmp(b, delta);
632 if (j <= 0) {
633 if (b->wds > 1 || b->x[0])
644 if (j < 0 || (j == 0 && !mode
649 if (rdir && (b->wds > 1 || b->x[0])) {
680 if (b->wds > 1 || b->x[0])
720 if (rdir == 2 || (b->wds <= 1 && !b->x[0]))
725 j = cmp(b, S);
727 if (j >= 0)
729 if (j > 0 || (j == 0 && dig & 1))
734 while(*--s == '9')
736 k++;
744 if (b->wds > 1 || b->x[0])
746 while(*--s == '0'){}
759 *decpt = k + 1;