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 "."). */
34 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
36 * Inspired by "How to Print Floating-Point Numbers Accurately" by
37 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
41 * to determine k = floor(log10(d)). We scale relevant
42 * quantities using O(log2(k)) rather than O(k) multiplications.
43 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
44 * try to generate digits strictly left to right. Instead, we
50 * round-nearest rule will give the same floating-point value
55 * 5. When converting floating-point integers less than 1e16,
56 * we use floating-point arithmetic rather than resorting
57 * to multiple-precision integers.
58 * 6. When asked to produce fewer than 15 digits, we first try
59 * to get by with floating-point arithmetic; we resort to
60 * multiple-precision integer arithmetic only if we cannot
61 * guarantee that the floating-point calculation has given
62 * the correctly rounded result. For k requested digits and
64 * something like 10^(k-15) that we must resort to the Long
84 /* Arguments ndigits, decpt, sign are similar to those
86 the returned string. If not null, *rve is set to point
87 to the end of the return value. If d is +-Infinity or NaN,
88 then *decpt is set to 9999.
92 and rounded to nearest.
97 return value similar to that of ecvt, except
100 gives a return value similar to that from fcvt,
103 4,5 ==> similar to 2 and 3, respectively, but (in
104 round-nearest mode) with the tests of mode 0 to
105 possibly return a shorter string that rounds to d.
107 -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
109 6-9 ==> Debugging modes similar to mode - 4: don't try
110 fast floating-point estimate (if applicable).
112 Values of mode other than 0-9 are treated as mode 0.
114 Sufficient space is allocated to the return value
115 to hold the suppressed trailing zeros.
119 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5, local
210 if (( j = 11 - hi0bits(word0(&d2) & Frac_mask) )!=0)
211 dval(&d2) /= 1 << j;
214 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
216 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
217 * log10(&d) = (i-Bias)*log(2)/log(10) + log10(&d2)
219 * This suggests computing an approximation k to log10(&d) by
221 * k = (i - Bias)*0.301029995663981
222 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
224 * We want k to be too large rather than too small.
225 * The error in the first-order Taylor series approximation
227 * to compensate for any error in the multiplication of
228 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
229 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
230 * adding 1e-13 to the constant term more than suffices.
231 * Hence we adjust the constant term to 0.1760912590558.
232 * (We could get a more accurate k by invoking log10,
236 i -= Bias;
239 i += j;
247 i = bbits + be + (Bias + (P-1) - 1);
248 x = i > 32 ? word0(&d) << (64 - i) | word1(&d) >> (i - 32)
249 : word1(&d) << (32 - i);
251 word0(&d2) -= 31*Exp_msk1; /* adjust exponent */
252 i -= (Bias + (P-1) - 1) + 1;
256 ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
257 k = (int)ds;
258 if (ds < 0. && ds != k)
259 k--; /* want k = floor(ds) */
261 if (k >= 0 && k <= Ten_pmax) {
262 if (dval(&d) < tens[k])
263 k--;
266 j = bbits - i - 1;
267 if (j >= 0) {
269 s2 = j;
272 b2 = -j;
275 if (k >= 0) {
277 s5 = k;
278 s2 += k;
281 b2 -= k;
282 b5 = -k;
297 mode -= 4;
301 ilim = ilim1 = -1; /* Values for cases 0 and 1; done here to */
302 /* silence erroneous "gcc -Wall" warning. */
321 i = ndigits + k + 1;
323 ilim1 = i - 1;
336 /* Try to get by with floating-point arithmetic. */
340 k0 = k;
343 if (k > 0) {
344 ds = tens[k&0xf];
345 j = k >> 4;
346 if (j & Bletch) {
348 j &= Bletch - 1;
349 dval(&d) /= bigtens[n_bigtens-1];
352 for(; j; j >>= 1, i++)
353 if (j & 1) {
359 else if (( j1 = -k )!=0) {
361 for(j = j1 >> 4; j; j >>= 1, i++)
362 if (j & 1) {
371 k--;
376 word0(&eps) -= (P-1)*Exp_msk1;
379 dval(&d) -= 5.;
382 if (dval(&d) < -dval(&eps))
391 dval(&eps) = 0.5/tens[ilim-1] - dval(&eps);
394 dval(&d) -= L;
398 if (1. - dval(&d) < dval(&eps))
409 dval(&eps) *= tens[ilim-1];
412 if (!(dval(&d) -= L))
418 else if (dval(&d) < 0.5 - dval(&eps)) {
419 while(*--s == '0');
432 k = k0;
438 if (be >= 0 && k <= Int_max) {
440 ds = tens[k];
449 dval(&d) -= L*ds;
453 L--;
480 while(*--s == '9')
482 k++;
500 denorm ? be + (Bias + (P-1) - 1 + 1) :
503 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
505 1 + P - bbits;
513 b2 -= i;
514 m2 -= i;
515 s2 -= i;
525 if (( j = b5 - m5 )!=0)
526 b = pow5mult(b, j);
559 * and for all and pass them and a shift to quorem, so it
560 * can do shifts and ors to compute the numerator for q.
563 if (( i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f )!=0)
564 i = 32 - i;
566 if (( i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf )!=0)
567 i = 16 - i;
570 i -= 4;
587 k--;
588 b = multadd(b, 10, 0); /* we botched the k estimate */
598 k = -1 - ndigits;
603 k++;
610 /* Compute mlo -- check for special case
616 mhi = Balloc(mhi->k);
624 * that will round to d?
626 j = cmp(b, mlo);
628 j1 = delta->sign ? 1 : cmp(b, delta);
638 if (j > 0)
641 else if (!b->x[0] && b->wds <= 1)
648 if (j < 0 || (j == 0 && mode != 1
653 if (!b->x[0] && b->wds <= 1) {
712 if (!b->x[0] && b->wds <= 1) {
732 j = cmp(b, S);
734 if (j >= 0)
736 if (j > 0 || (j == 0 && dig & 1))
740 while(*--s == '9')
742 k++;
752 while(*--s == '0');
776 *decpt = k + 1;