|
|
1.1 ! root 1: /* ftoa from portable c library, modified to use faster multiply ! 2: up and down. */ ! 3: /* mjm: removed printf and _dummy call at end; ! 4: * now in separate file, rhprintf.o ! 5: */ ! 6: ! 7: ftoa (x, str, prec, format) ! 8: float x; ! 9: char *str; ! 10: { ! 11: /* converts a floating point number to an ascii string */ ! 12: /* x is stored into str, which should be at least 30 chars long */ ! 13: int ie, i, k, ndig, fstyle; ! 14: double y; ! 15: ndig = ( prec<0) ? 7 : (prec > 22 ? 23 : prec+1); ! 16: if (format == 'f' || format == 'F') ! 17: fstyle = 1; ! 18: else ! 19: fstyle = 0; ! 20: /* print in e format unless last arg is 'f' */ ! 21: ie = 0; ! 22: /* if x negative, write minus and reverse */ ! 23: if ( x < 0.0) ! 24: { ! 25: *str++ = '-'; ! 26: x = -x; ! 27: } ! 28: ! 29: /* put x in range 1 <= x < 10 */ ! 30: if(x > 0.0) { ! 31: if(x<1e-32){x=x* 1e33; ie = ie - 33;} ! 32: if(x<1e-16){x=x* 1e17; ie = ie - 17;} ! 33: if(x<1e-8){x=x* 1e9; ie = ie - 9;} ! 34: if(x<1e-4){x=x* 1e5; ie = ie - 5;} ! 35: if(x<1e-2){x=x* 1e3; ie = ie - 3;} ! 36: if(x<1e-1){x=x* 1e2; ie = ie - 2;} ! 37: while(x<1.0){ x=x* 10.; ie = ie - 1;} ! 38: } ! 39: if(x>=1e32){x=x/1e32; ie = ie + 32;} ! 40: if(x>=1e16){x=x/1e16; ie = ie + 16;} ! 41: if(x>=1e8){x=x/1e8; ie = ie + 8;} ! 42: if(x>=1e4){x=x/1e4; ie = ie + 4;} ! 43: if(x>=1e2){x=x/1e2; ie = ie + 2;} ! 44: while(x>=10.){x=x/10.;ie++;} ! 45: ! 46: /* in f format, number of digits is related to size */ ! 47: if (fstyle) ndig = ndig + ie; ! 48: ! 49: /* round. x is between 1 and 10 and ndig will be printed to ! 50: right of decimal point so rounding is ... */ ! 51: y = 10.0; ! 52: for (i = 0; i < ndig; i++) ! 53: y=y/10.0; ! 54: x=x+.5*y; ! 55: if (x >= 10.0) /* repair rounding disasters */ ! 56: { ! 57: x = 1.0; ! 58: ie++; ! 59: if (fstyle) ndig++; ! 60: } ! 61: /* now loop. put out a digit (obtain by multiplying by ! 62: 10, truncating, subtracting) until enough digits out */ ! 63: /* if fstyle, and leading zeros, they go out special */ ! 64: if (fstyle && ie < 0) ! 65: { ! 66: *str++ = '0'; ! 67: i = (ndig > 0) ? -1 : (ndig-1); ! 68: i = i - ie; ! 69: if (i > 0) ! 70: { ! 71: *str++ = '.'; ! 72: while (i--) ! 73: *str++ = '0'; ! 74: } ! 75: } ! 76: for (i=0; i < ndig; i++) ! 77: { ! 78: if (i == (fstyle ? ie+1 : 1)) /* where is decimal point */ ! 79: *str++ = '.'; ! 80: k = x; ! 81: *str++ = k + '0'; ! 82: x = x - (y=k); ! 83: x=x*10.; ! 84: } ! 85: ! 86: /* now, in estyle, put out exponent if not zero */ ! 87: if (!fstyle && ie != 0) ! 88: { ! 89: *str++ = 'E'; ! 90: if (ie < 0) ! 91: { ! 92: ie = -ie; ! 93: *str++ = '-'; ! 94: } ! 95: for (k=100; k > ie; k = k/10); ! 96: for (; k > 0; k = k/10) ! 97: { ! 98: *str++ = ie/k + '0'; ! 99: ie = ie%k; ! 100: } ! 101: } ! 102: *str = '\0'; ! 103: return; ! 104: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.