1 /* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */
24 * first few powers of 10, enough for about 1/2 of the
25 * total space for doubles.
27 static double pows10[] =
29 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
30 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
31 1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
32 1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39,
33 1e40, 1e41, 1e42, 1e43, 1e44, 1e45, 1e46, 1e47, 1e48, 1e49,
34 1e50, 1e51, 1e52, 1e53, 1e54, 1e55, 1e56, 1e57, 1e58, 1e59,
35 1e60, 1e61, 1e62, 1e63, 1e64, 1e65, 1e66, 1e67, 1e68, 1e69,
36 1e70, 1e71, 1e72, 1e73, 1e74, 1e75, 1e76, 1e77, 1e78, 1e79,
37 1e80, 1e81, 1e82, 1e83, 1e84, 1e85, 1e86, 1e87, 1e88, 1e89,
38 1e90, 1e91, 1e92, 1e93, 1e94, 1e95, 1e96, 1e97, 1e98, 1e99,
39 1e100, 1e101, 1e102, 1e103, 1e104, 1e105, 1e106, 1e107, 1e108, 1e109,
40 1e110, 1e111, 1e112, 1e113, 1e114, 1e115, 1e116, 1e117, 1e118, 1e119,
41 1e120, 1e121, 1e122, 1e123, 1e124, 1e125, 1e126, 1e127, 1e128, 1e129,
42 1e130, 1e131, 1e132, 1e133, 1e134, 1e135, 1e136, 1e137, 1e138, 1e139,
43 1e140, 1e141, 1e142, 1e143, 1e144, 1e145, 1e146, 1e147, 1e148, 1e149,
44 1e150, 1e151, 1e152, 1e153, 1e154, 1e155, 1e156, 1e157, 1e158, 1e159,
46 #define npows10 ((int)(sizeof(pows10)/sizeof(pows10[0])))
47 #define pow10(x) fmtpow10(x)
64 d = pows10[npows10-1];
71 d *= pows10[npows10 - 1];
80 * add 1 to the decimal integer string a of length n.
81 * if 99999 overflows into 10000, return 1 to tell caller
82 * to move the virtual decimal point.
90 if(n < 0 || n > NSIGNIF)
92 for(b = a+n-1; b >= a; b--) {
101 * need to overflow adding digit.
102 * shift number down and insert 1 at beginning.
103 * decimal is known to be 0s or we wouldn't
104 * have gotten this far. (e.g., 99999+1 => 00000)
111 * subtract 1 from the decimal integer string a.
112 * if 10000 underflows into 09999, make it 99999
113 * and return 1 to tell caller to move the virtual
114 * decimal point. this way, xsub1 is inverse of xadd1.
117 xsub1(char *a, int n)
122 if(n < 0 || n > NSIGNIF)
124 for(b = a+n-1; b >= a; b--) {
127 if(c == '0' && b == a) {
129 * just zeroed the top digit; shift everyone up.
130 * decimal is known to be 9s or we wouldn't
131 * have gotten this far. (e.g., 10000-1 => 09999)
142 * can't get here. the number a is always normalized
143 * so that it has a nonzero first digit.
149 * format exponent like sprintf(p, "e%+02d", e)
152 xfmtexp(char *p, int e, int ucase)
157 *p++ = ucase ? 'E' : 'e';
165 se[i++] = e % 10 + '0';
176 * compute decimal integer m, exp such that:
178 * m is as short as possible with losing exactness
179 * assumes special cases (NaN, +Inf, -Inf) have been handled.
182 xdtoa(double f, char *s, int *exp, int *neg, int *ns)
184 int c, d, e2, e, ee, i, ndigit, oerrno;
185 char tmp[NSIGNIF+10];
188 oerrno = errno; /* in case strtod smashes errno */
191 * make f non-negative.
200 * must handle zero specially.
211 * find g,e such that f = g*10^e.
212 * guess 10-exponent using 2-exponent, then fine tune.
215 e = (int)(e2 * .301029995664);
227 * convert NSIGNIF digits as a first approximation.
229 for(i=0; i<NSIGNIF; i++) {
237 * adjust e because s is 314159... not 3.14159...
240 xfmtexp(s+NSIGNIF, e, 0);
243 * adjust conversion until strtod(s) == f exactly.
245 for(i=0; i<10; i++) {
246 g = fmtstrtod(s, nil);
248 if(xadd1(s, NSIGNIF)) {
251 xfmtexp(s+NSIGNIF, e, 0);
256 if(xsub1(s, NSIGNIF)) {
259 xfmtexp(s+NSIGNIF, e, 0);
267 * play with the decimal to try to simplify.
271 * bump last few digits up to 9 if we can
273 for(i=NSIGNIF-1; i>=NSIGNIF-3; i--) {
277 g = fmtstrtod(s, nil);
286 * add 1 in hopes of turning 9s to 0s
288 if(s[NSIGNIF-1] == '9') {
291 if(xadd1(tmp, NSIGNIF)) {
293 xfmtexp(tmp+NSIGNIF, ee, 0);
295 g = fmtstrtod(tmp, nil);
303 * bump last few digits down to 0 as we can.
305 for(i=NSIGNIF-1; i>=NSIGNIF-3; i--) {
309 g = fmtstrtod(s, nil);
318 * remove trailing zeros.
321 while(ndigit > 1 && s[ndigit-1] == '0'){
332 static char *special[] = { "NaN", "NaN", "+Inf", "+Inf", "-Inf", "-Inf" };
334 static char *special[] = { "nan", "NAN", "inf", "INF", "-inf", "-INF" };
340 char buf[NSIGNIF+10], *dot, *digits, *p, *s, suf[10], *t;
342 int c, chr, dotwid, e, exp, fl, ndigits, neg, newndigits;
343 int pad, point, prec, realchr, sign, sufwid, ucase, wid, z1, z2;
346 if(fmt->flags&FmtLong)
347 f = va_arg(fmt->args, long double);
349 f = va_arg(fmt->args, double);
352 * extract formatting flags
372 * pick off special numbers.
375 s = special[0+ucase];
377 fmt->flags = fl & (FmtWidth|FmtLeft);
378 return __fmtcpy(fmt, s, strlen(s), strlen(s));
381 s = special[2+ucase];
385 s = special[4+ucase];
390 * get exact representation.
393 xdtoa(f, digits, &exp, &neg, &ndigits);
396 * get locale's decimal point.
401 dotwid = utflen(dot);
404 * now the formatting fun begins.
405 * compute parameters for actual fmt:
407 * pad: number of spaces to insert before/after field.
408 * z1: number of zeros to insert before digits
409 * z2: number of zeros to insert after digits
410 * point: number of digits to print before decimal point
411 * ndigits: number of digits to use from digits[]
412 * suf: trailing suffix, like "e-5"
418 * convert to at most prec significant digits. (prec=0 means 1)
423 if(digits[prec] >= '5' && xadd1(digits, prec))
430 * extra rules for %g (implemented below):
431 * trailing zeros removed after decimal unless FmtSharp.
432 * decimal point only if digit follows.
435 /* fall through to %e */
439 * one significant digit before decimal, no leading zeros.
445 * decimal point is after ndigits digits right now.
446 * slide to be after first.
448 e = exp + (ndigits-1);
451 * if this is %g, check exponent and convert prec
454 if(-4 <= e && e < prec)
456 prec--; /* one digit before decimal; rest after */
460 * compute trailing zero padding or truncate digits.
462 if(1+prec >= ndigits)
463 z2 = 1+prec - ndigits;
468 assert(realchr != 'g');
470 if(digits[newndigits] >= '5' && xadd1(digits, newndigits)) {
472 * had 999e4, now have 100e5
476 ndigits = newndigits;
479 xfmtexp(suf, e, ucase);
480 sufwid = strlen(suf);
486 * determine where digits go with respect to decimal point
488 if(ndigits+exp > 0) {
493 z1 = 1 + -(ndigits+exp);
497 * %g specifies prec = number of significant digits
498 * convert to number of digits after decimal point
504 * compute trailing zero padding or truncate digits.
506 if(point+prec >= z1+ndigits)
507 z2 = point+prec - (z1+ndigits);
512 assert(realchr != 'g');
513 newndigits = point+prec - z1;
517 } else if(newndigits == 0) {
518 /* perhaps round up */
519 if(digits[0] >= '5'){
524 } else if(digits[newndigits] >= '5' && xadd1(digits, newndigits)) {
526 * digits was 999, is now 100; make it 1000
528 digits[newndigits++] = '0';
531 * account for new digit
533 if(z1) /* 0.099 => 0.100 or 0.99 => 1.00*/
535 else /* 9.99 => 10.00 */
539 ndigits = newndigits;
546 * if %g is given without FmtSharp, remove trailing zeros.
547 * must do after truncation, so that e.g. print %.3g 1.001
548 * produces 1, not 1.00. sorry, but them's the rules.
550 if(realchr == 'g' && !(fl & FmtSharp)) {
551 if(z1+ndigits+z2 >= point) {
552 if(z1+ndigits < point)
553 z2 = point - (z1+ndigits);
556 while(z1+ndigits > point && digits[ndigits-1] == '0')
563 * compute width of all digits and decimal point and suffix if any
568 else if(wid == point){
572 point++; /* do not print any decimal point */
582 else if(fl & FmtSign)
584 else if(fl & FmtSpace)
593 if((fl & FmtWidth) && fmt->width > wid)
594 pad = fmt->width - wid;
595 if(pad && !(fl & FmtLeft) && (fl & FmtZero)){
602 * format the actual field. too bad about doing this twice.
605 if(pad && !(fl & FmtLeft) && __rfmtpad(fmt, pad) < 0)
608 rs = (Rune*)fmt->stop;
610 FMTRCHAR(fmt, rt, rs, sign);
611 while(z1>0 || ndigits>0 || z2>0) {
615 }else if(ndigits > 0){
622 FMTRCHAR(fmt, rt, rs, c);
625 p += chartorune(&r, p);
626 FMTRCHAR(fmt, rt, rs, r);
630 fmt->nfmt += rt - (Rune*)fmt->to;
632 if(sufwid && __fmtcpy(fmt, suf, sufwid, sufwid) < 0)
634 if(pad && (fl & FmtLeft) && __rfmtpad(fmt, pad) < 0)
637 if(pad && !(fl & FmtLeft) && __fmtpad(fmt, pad) < 0)
640 s = (char*)fmt->stop;
642 FMTCHAR(fmt, t, s, sign);
643 while(z1>0 || ndigits>0 || z2>0) {
647 }else if(ndigits > 0){
654 FMTCHAR(fmt, t, s, c);
657 FMTCHAR(fmt, t, s, *p);
659 fmt->nfmt += t - (char*)fmt->to;
661 if(sufwid && __fmtcpy(fmt, suf, sufwid, sufwid) < 0)
663 if(pad && (fl & FmtLeft) && __fmtpad(fmt, pad) < 0)