|
|
1.1 ! root 1: /* ftoa from portable c library, modified to use faster multiply ! 2: up and down. */ ! 3: ! 4: ftoa (x, str, prec, format) ! 5: float x; ! 6: char *str; ! 7: { ! 8: /* converts a floating point number to an ascii string */ ! 9: /* x is stored into str, which should be at least 30 chars long */ ! 10: int ie, i, k, ndig, fstyle; ! 11: double y; ! 12: ndig = ( prec<0) ? 7 : (prec > 22 ? 23 : prec+1); ! 13: if (format == 'f' || format == 'F') ! 14: fstyle = 1; ! 15: else ! 16: fstyle = 0; ! 17: /* print in e format unless last arg is 'f' */ ! 18: ie = 0; ! 19: /* if x negative, write minus and reverse */ ! 20: if ( x < 0.0) ! 21: { ! 22: *str++ = '-'; ! 23: x = -x; ! 24: } ! 25: ! 26: /* put x in range 1 <= x < 10 */ ! 27: if(x > 0.0) { ! 28: if(x<1e-32){ ! 29: x=x* 1e33; ! 30: ie -= 33; ! 31: } ! 32: if(x<1e-16){ ! 33: x=x* 1e17; ! 34: ie -= 17; ! 35: } ! 36: if(x<1e-8){ ! 37: x=x* 1e9; ! 38: ie -= 9; ! 39: } ! 40: if(x<1e-4){ ! 41: x=x* 1e5; ! 42: ie -= 5; ! 43: } ! 44: if(x<1e-2){ ! 45: x=x* 1e3; ! 46: ie -= 3; ! 47: } ! 48: if(x<1e-1){ ! 49: x=x* 1e2; ! 50: ie -= 2; ! 51: } ! 52: while(x<1.0){ ! 53: x=x* 10.; ! 54: ie -= 1; ! 55: } ! 56: } ! 57: if(x>=1e32){ ! 58: x=x/1e32; ! 59: ie += 32; ! 60: } ! 61: if(x>=1e16){ ! 62: x=x/1e16; ! 63: ie += 16; ! 64: } ! 65: if(x>=1e8){ ! 66: x=x/1e8; ! 67: ie += 8; ! 68: } ! 69: if(x>=1e4){ ! 70: x=x/1e4; ! 71: ie += 4; ! 72: } ! 73: if(x>=1e2){ ! 74: x=x/1e2; ! 75: ie += 2; ! 76: } ! 77: while(x>=10.){ ! 78: x=x/10.; ! 79: ie++; ! 80: } ! 81: ! 82: /* in f format, number of digits is related to size */ ! 83: if (fstyle) ndig += ie; ! 84: ! 85: /* round. x is between 1 and 10 and ndig will be printed to ! 86: right of decimal point so rounding is ... */ ! 87: y = 10.0; ! 88: for (i = 0; i < ndig; i++) ! 89: y=y/10.0; ! 90: x=x+.5*y; ! 91: if (x >= 10.0) /* repair rounding disasters */ ! 92: { ! 93: x = 1.0; ! 94: ie++; ! 95: if (fstyle) ndig++; ! 96: } ! 97: /* now loop. put out a digit (obtain by multiplying by ! 98: 10, truncating, subtracting) until enough digits out */ ! 99: /* if fstyle, and leading zeros, they go out special */ ! 100: if (fstyle && ie < 0) ! 101: { ! 102: *str++ = '0'; ! 103: i = (ndig > 0) ? -1 : (ndig-1); ! 104: i -= ie; ! 105: if (i > 0) ! 106: { ! 107: *str++ = '.'; ! 108: while (i--) ! 109: *str++ = '0'; ! 110: } ! 111: } ! 112: for (i=0; i < ndig; i++) ! 113: { ! 114: if (i == (fstyle ? ie+1 : 1)) /* where is decimal point */ ! 115: *str++ = '.'; ! 116: k = x; ! 117: *str++ = k + '0'; ! 118: x -= (y=k); ! 119: x=x*10.; ! 120: } ! 121: ! 122: /* now, in estyle, put out exponent if not zero */ ! 123: if (!fstyle && ie != 0) ! 124: { ! 125: *str++ = 'E'; ! 126: if (ie < 0) ! 127: { ! 128: ie = -ie; ! 129: *str++ = '-'; ! 130: } ! 131: for (k=100; k > ie; k /=10); ! 132: for (; k > 0; k /=10) ! 133: { ! 134: *str++ = ie/k + '0'; ! 135: ie = ie%k; ! 136: } ! 137: } ! 138: *str = '\0'; ! 139: return; ! 140: } ! 141: ! 142: /* printf here to force loading of pfloat is printf is called. */ ! 143: ! 144: printf(arg0) ! 145: char *arg0; ! 146: { ! 147: extern int putchar(); ! 148: return(format(putchar,"%r",&arg0)); ! 149: } ! 150: ! 151: __dummy() { ! 152: extern pfloat(); /* force pfloat and pscien to be loaded */ ! 153: pfloat(); ! 154: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.