static char* xdodtoa(char *s1, double f, int chr, int prec, int *decpt, int *rsign) { char s2[NSIGNIF+10]; double g, h; int e, d, i; int c2, sign, oerr; if(chr == 'F') chr = 'f'; if(prec > NSIGNIF) prec = NSIGNIF; if(prec < 0) prec = 0; if(__isNaN(f)) { *decpt = 9999; *rsign = 0; strcpy(s1, "nan"); return &s1[3]; } sign = 0; if(f < 0) { f = -f; sign++; } *rsign = sign; if(__isInf(f, 1) || __isInf(f, -1)) { *decpt = 9999; strcpy(s1, "inf"); return &s1[3]; } e = 0; g = f; if(g != 0) { frexp(f, &e); e = (int)(e * .301029995664); if(e >= -150 && e <= +150) { d = 0; h = f; } else { d = e/2; h = f * pow10(-d); } g = h * pow10(d-e); while(g < 1) { e--; g = h * pow10(d-e); } while(g >= 10) { e++; g = h * pow10(d-e); } } /* * convert NSIGNIF digits and convert * back to get accuracy. */ for(i=0; i<NSIGNIF; i++) { d = (int)g; s1[i] = d + '0'; g = (g - d) * 10; } s1[i] = 0; /* * try decimal rounding to eliminate 9s */ c2 = prec + 1; if(chr == 'f') c2 += e; oerr = errno; if(c2 >= NSIGNIF-2) { strcpy(s2, s1); d = e; s1[NSIGNIF-2] = '0'; s1[NSIGNIF-1] = '0'; xaddexp(s1+NSIGNIF, e-NSIGNIF+1); g = fmtstrtod(s1, nil); if(g == f) goto found; if(xadd(s1, NSIGNIF-3, 1)) { e++; xaddexp(s1+NSIGNIF, e-NSIGNIF+1); } g = fmtstrtod(s1, nil); if(g == f) goto found; strcpy(s1, s2); e = d; } /* * convert back so s1 gets exact answer */ for(d = 0; d < 10; d++) { xaddexp(s1+NSIGNIF, e-NSIGNIF+1); g = fmtstrtod(s1, nil); if(f > g) { if(xadd(s1, NSIGNIF-1, 1)) e--; continue; } if(f < g) { if(xsub(s1, NSIGNIF-1, 1)) e++; continue; } break; } found: errno = oerr; /* * sign */ d = 0; i = 0; /* * round & adjust 'f' digits */ c2 = prec + 1; if(chr == 'f'){ if(xadd(s1, c2+e, 5)) e++; c2 += e; if(c2 < 0){ c2 = 0; e = -prec - 1; } }else{ if(xadd(s1, c2, 5)) e++; } if(c2 > NSIGNIF){ c2 = NSIGNIF; } *decpt = e + 1; /* * terminate the converted digits */ s1[c2] = '\0'; return &s1[c2]; }
static void xdtoa(Fmt *fmt, char *s2, double f) { char s1[NSIGNIF+10]; double g, h; int e, d, i, n; int c1, c2, c3, c4, ucase, sign, chr, prec; prec = FDEFLT; if(fmt->flags & FmtPrec) prec = fmt->prec; if(prec > FDIGIT) prec = FDIGIT; if(__isNaN(f)) { strcpy(s2, "NaN"); return; } if(__isInf(f, 1)) { strcpy(s2, "+Inf"); return; } if(__isInf(f, -1)) { strcpy(s2, "-Inf"); return; } sign = 0; if(f < 0) { f = -f; sign++; } ucase = 0; chr = fmt->r; if(isupper(chr)) { ucase = 1; chr = tolower(chr); } e = 0; g = f; if(g != 0) { frexp(f, &e); e = e * .301029995664; if(e >= -150 && e <= +150) { d = 0; h = f; } else { d = e/2; h = f * pow10(-d); } g = h * pow10(d-e); while(g < 1) { e--; g = h * pow10(d-e); } while(g >= 10) { e++; g = h * pow10(d-e); } } /* * convert NSIGNIF digits and convert * back to get accuracy. */ for(i=0; i<NSIGNIF; i++) { d = g; s1[i] = d + '0'; g = (g - d) * 10; } s1[i] = 0; /* * try decimal rounding to eliminate 9s */ c2 = prec + 1; if(chr == 'f') c2 += e; if(c2 >= NSIGNIF-2) { strcpy(s2, s1); d = e; s1[NSIGNIF-2] = '0'; s1[NSIGNIF-1] = '0'; sprint(s1+NSIGNIF, "e%d", e-NSIGNIF+1); g = strtod(s1, nil); if(g == f) goto found; if(xadd(s1, NSIGNIF-3, 1)) { e++; sprint(s1+NSIGNIF, "e%d", e-NSIGNIF+1); } g = strtod(s1, nil); if(g == f) goto found; strcpy(s1, s2); e = d; } /* * convert back so s1 gets exact answer */ for(;;) { sprint(s1+NSIGNIF, "e%d", e-NSIGNIF+1); g = strtod(s1, nil); if(f > g) { if(xadd(s1, NSIGNIF-1, 1)) e--; continue; } if(f < g) { if(xsub(s1, NSIGNIF-1, 1)) e++; continue; } break; } found: /* * sign */ d = 0; i = 0; if(sign) s2[d++] = '-'; else if(fmt->flags & FmtSign) s2[d++] = '+'; else if(fmt->flags & FmtSpace) s2[d++] = ' '; /* * copy into final place * c1 digits of leading '0' * c2 digits from conversion * c3 digits of trailing '0' * c4 digits after '.' */ c1 = 0; c2 = prec + 1; c3 = 0; c4 = prec; switch(chr) { default: if(xadd(s1, c2, 5)) e++; break; case 'g': /* * decide on 'e' of 'f' style convers */ if(xadd(s1, c2, 5)) e++; if(e >= -5 && e <= prec) { c1 = -e - 1; c4 = prec - e; chr = 'h'; // flag for 'f' style } break; case 'f': if(xadd(s1, c2+e, 5)) e++; c1 = -e; if(c1 > prec) c1 = c2; c2 += e; break; } /* * clean up c1 c2 and c3 */ if(c1 < 0) c1 = 0; if(c2 < 0) c2 = 0; if(c2 > NSIGNIF) { c3 = c2-NSIGNIF; c2 = NSIGNIF; } /* * copy digits */ while(c1 > 0) { if(c1+c2+c3 == c4) s2[d++] = '.'; s2[d++] = '0'; c1--; } while(c2 > 0) { if(c2+c3 == c4) s2[d++] = '.'; s2[d++] = s1[i++]; c2--; } while(c3 > 0) { if(c3 == c4) s2[d++] = '.'; s2[d++] = '0'; c3--; } /* * strip trailing '0' on g conv */ if(fmt->flags & FmtSharp) { if(0 == c4) s2[d++] = '.'; } else if(chr == 'g' || chr == 'h') { for(n=d-1; n>=0; n--) if(s2[n] != '0') break; for(i=n; i>=0; i--) if(s2[i] == '.') { d = n; if(i != n) d++; break; } } if(chr == 'e' || chr == 'g') { if(ucase) s2[d++] = 'E'; else s2[d++] = 'e'; c1 = e; if(c1 < 0) { s2[d++] = '-'; c1 = -c1; } else s2[d++] = '+'; if(c1 >= 100) { s2[d++] = c1/100 + '0'; c1 = c1%100; } s2[d++] = c1/10 + '0'; s2[d++] = c1%10 + '0'; } s2[d] = 0; }