2 * The authors of this software are Rob Pike and Ken Thompson.
3 * Copyright (c) 2002 by Lucent Technologies.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose without fee is hereby granted, provided that this entire notice
6 * is included in all copies of any software which is or includes a copy
7 * or modification of this software and in all copies of the supporting
8 * documentation for such software.
9 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
10 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
11 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
12 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
32 * first few powers of 10, enough for about 1/2 of the
33 * total space for doubles.
35 static double pows10[] =
37 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
38 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
39 1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
40 1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39,
41 1e40, 1e41, 1e42, 1e43, 1e44, 1e45, 1e46, 1e47, 1e48, 1e49,
42 1e50, 1e51, 1e52, 1e53, 1e54, 1e55, 1e56, 1e57, 1e58, 1e59,
43 1e60, 1e61, 1e62, 1e63, 1e64, 1e65, 1e66, 1e67, 1e68, 1e69,
44 1e70, 1e71, 1e72, 1e73, 1e74, 1e75, 1e76, 1e77, 1e78, 1e79,
45 1e80, 1e81, 1e82, 1e83, 1e84, 1e85, 1e86, 1e87, 1e88, 1e89,
46 1e90, 1e91, 1e92, 1e93, 1e94, 1e95, 1e96, 1e97, 1e98, 1e99,
47 1e100, 1e101, 1e102, 1e103, 1e104, 1e105, 1e106, 1e107, 1e108, 1e109,
48 1e110, 1e111, 1e112, 1e113, 1e114, 1e115, 1e116, 1e117, 1e118, 1e119,
49 1e120, 1e121, 1e122, 1e123, 1e124, 1e125, 1e126, 1e127, 1e128, 1e129,
50 1e130, 1e131, 1e132, 1e133, 1e134, 1e135, 1e136, 1e137, 1e138, 1e139,
51 1e140, 1e141, 1e142, 1e143, 1e144, 1e145, 1e146, 1e147, 1e148, 1e149,
52 1e150, 1e151, 1e152, 1e153, 1e154, 1e155, 1e156, 1e157, 1e158, 1e159,
63 if(n < DBL_MIN_10_EXP){
68 }else if(n > DBL_MAX_10_EXP){
71 if(n < (int)(sizeof(pows10)/sizeof(pows10[0])))
74 d = pows10[sizeof(pows10)/sizeof(pows10[0]) - 1];
76 n -= sizeof(pows10)/sizeof(pows10[0]) - 1;
77 if(n < (int)(sizeof(pows10)/sizeof(pows10[0]))){
81 d *= pows10[sizeof(pows10)/sizeof(pows10[0]) - 1];
91 xadd(char *a, int n, int v)
96 if(n < 0 || n >= NSIGNIF)
98 for(b = a+n; b >= a; b--) {
107 *a = '1'; /* overflow adding */
112 xsub(char *a, int n, int v)
117 for(b = a+n; b >= a; b--) {
126 *a = '9'; /* underflow subtracting */
131 xaddexp(char *p, int e)
143 se[i++] = e % 10 + '0';
156 xdodtoa(char *s1, double f, int chr, int prec, int *decpt, int *rsign)
181 if(__isInf(f, 1) || __isInf(f, -1)) {
191 e = (int)(e * .301029995664);
192 if(e >= -150 && e <= +150) {
211 * convert NSIGNIF digits and convert
212 * back to get accuracy.
214 for(i=0; i<NSIGNIF; i++) {
222 * try decimal rounding to eliminate 9s
228 if(c2 >= NSIGNIF-2) {
233 xaddexp(s1+NSIGNIF, e-NSIGNIF+1);
234 g = fmtstrtod(s1, nil);
237 if(xadd(s1, NSIGNIF-3, 1)) {
239 xaddexp(s1+NSIGNIF, e-NSIGNIF+1);
241 g = fmtstrtod(s1, nil);
249 * convert back so s1 gets exact answer
251 for(d = 0; d < 10; d++) {
252 xaddexp(s1+NSIGNIF, e-NSIGNIF+1);
253 g = fmtstrtod(s1, nil);
255 if(xadd(s1, NSIGNIF-1, 1))
260 if(xsub(s1, NSIGNIF-1, 1))
277 * round & adjust 'f' digits
281 if(xadd(s1, c2+e, 5))
299 * terminate the converted digits
306 * this function works like the standard dtoa, if you want it.
310 __dtoa(double f, int mode, int ndigits, int *decpt, int *rsign, char **rve)
312 static char s2[NSIGNIF + 10];
339 if(chr != 'f' && ndigits){
347 es = xdodtoa(s2, f, chr, prec, decpt, rsign);
352 for(; es > s2 + 1; es--){
365 fmtzdotpad(Fmt *f, int n, int pt)
374 for(i = 0; i < n; i++){
376 FMTRCHAR(f, rt, rs, '.');
378 FMTRCHAR(f, rt, rs, '0');
380 f->nfmt += rt - (Rune*)f->to;
385 for(i = 0; i < n; i++){
387 FMTCHAR(f, t, s, '.');
389 FMTCHAR(f, t, s, '0');
391 f->nfmt += t - (char *)f->to;
403 int c1, c2, c3, c4, ucase, sign, chr, prec, fl;
405 f = va_arg(fmt->args, double);
416 }else if(chr == 'F'){
419 }else if(chr == 'G'){
423 if(prec > 0 && chr == 'g')
428 xdodtoa(s1, f, chr, prec, &e, &sign);
430 if(*s1 == 'i' || *s1 == 'n'){
438 fmt->flags = fl & (FmtWidth|FmtLeft);
439 return __fmtcpy(fmt, (const void*)s1, 3, 3);
443 * copy into final place
444 * c1 digits of leading '0'
445 * c2 digits from conversion
446 * c3 digits of trailing '0'
447 * c4 digits after '.'
459 * decide on 'e' of 'f' style convers
461 if(e >= -4 && e <= prec) {
464 chr = 'h'; /* flag for 'f' style */
476 * clean up c1 c2 and c3
488 * trim trailing zeros for %g
491 && (chr == 'g' || chr == 'h')){
499 while(c4 && c2 > 1 && s1[c2 - 1] == '0'){
506 * calculate the total length
509 if(sign || (fl & (FmtSign|FmtSpace)))
511 if(c4 || (fl & FmtSharp)){
514 if(chr == 'e' || chr == 'g'){
521 * pad to width if right justified
523 if((fl & (FmtWidth|FmtLeft)) == FmtWidth && n < fmt->width){
525 c1 += fmt->width - n;
527 if(__fmtpad(fmt, fmt->width - n) < 0){
539 else if(fl & FmtSign)
541 else if(fl & FmtSpace)
543 if(d && fmtrune(fmt, d) < 0){
550 c4 = c1 + c2 + c3 - c4;
552 if(fmtzdotpad(fmt, c1, c4) < 0){
558 if(c4 >= 0 && c4 < c2){
559 if(__fmtcpy(fmt, s1, c4, c4) < 0 || fmtrune(fmt, '.') < 0)
565 if(__fmtcpy(fmt, (const void*)(s1 + d), c2, c2) < 0){
570 if(fmtzdotpad(fmt, c3, c4) < 0){
577 * strip trailing '0' on g conv
579 if((fl & FmtSharp) && c4 == 0 && fmtrune(fmt, '.') < 0){
582 if(chr == 'e' || chr == 'g') {
595 s1[d++] = c1/100 + '0';
598 s1[d++] = c1/10 + '0';
599 s1[d++] = c1%10 + '0';
600 if(__fmtcpy(fmt, s1, d, d) < 0){
604 if((fl & (FmtWidth|FmtLeft)) == (FmtWidth|FmtLeft) && n < fmt->width){
605 if(__fmtpad(fmt, fmt->width - n) < 0){