|
|
1.1 ! root 1: /* @(#)ecvt.c 4.1 (Berkeley) 12/21/80 */ ! 2: /* ! 3: * ecvt converts to decimal ! 4: * the number of digits is specified by ndigit ! 5: * decpt is set to the position of the decimal point ! 6: * sign is set to 0 for positive, 1 for negative ! 7: */ ! 8: ! 9: char *cvt(); ! 10: ! 11: #define NDIG 80 ! 12: char* ! 13: ecvt(arg, ndigits, decpt, sign) ! 14: double arg; ! 15: int ndigits, *decpt, *sign; ! 16: { ! 17: return(cvt(arg, ndigits, decpt, sign, 1)); ! 18: } ! 19: ! 20: char* ! 21: fcvt(arg, ndigits, decpt, sign) ! 22: double arg; ! 23: int ndigits, *decpt, *sign; ! 24: { ! 25: return(cvt(arg, ndigits, decpt, sign, 0)); ! 26: } ! 27: ! 28: static char* ! 29: cvt(arg, ndigits, decpt, sign, eflag) ! 30: double arg; ! 31: int ndigits, *decpt, *sign; ! 32: { ! 33: register int r2; ! 34: double fi, fj; ! 35: register char *p, *p1; ! 36: static char buf[NDIG]; ! 37: double modf(); ! 38: ! 39: if (ndigits<0) ! 40: ndigits = 0; ! 41: if (ndigits>=NDIG-1) ! 42: ndigits = NDIG-2; ! 43: r2 = 0; ! 44: *sign = 0; ! 45: p = &buf[0]; ! 46: if (arg<0) { ! 47: *sign = 1; ! 48: arg = -arg; ! 49: } ! 50: arg = modf(arg, &fi); ! 51: p1 = &buf[NDIG]; ! 52: /* ! 53: * Do integer part ! 54: */ ! 55: if (fi != 0) { ! 56: p1 = &buf[NDIG]; ! 57: while (fi != 0) { ! 58: fj = modf(fi/10, &fi); ! 59: *--p1 = (int)((fj+.03)*10) + '0'; ! 60: r2++; ! 61: } ! 62: while (p1 < &buf[NDIG]) ! 63: *p++ = *p1++; ! 64: } else if (arg > 0) { ! 65: while ((fj = arg*10) < 1) { ! 66: arg = fj; ! 67: r2--; ! 68: } ! 69: } ! 70: p1 = &buf[ndigits]; ! 71: if (eflag==0) ! 72: p1 += r2; ! 73: *decpt = r2; ! 74: if (p1 < &buf[0]) { ! 75: buf[0] = '\0'; ! 76: return(buf); ! 77: } ! 78: while (p<=p1 && p<&buf[NDIG]) { ! 79: arg *= 10; ! 80: arg = modf(arg, &fj); ! 81: *p++ = (int)fj + '0'; ! 82: } ! 83: if (p1 >= &buf[NDIG]) { ! 84: buf[NDIG-1] = '\0'; ! 85: return(buf); ! 86: } ! 87: p = p1; ! 88: *p1 += 5; ! 89: while (*p1 > '9') { ! 90: *p1 = '0'; ! 91: if (p1>buf) ! 92: ++*--p1; ! 93: else { ! 94: *p1 = '1'; ! 95: (*decpt)++; ! 96: if (eflag==0) { ! 97: if (p>buf) ! 98: *p = '0'; ! 99: p++; ! 100: } ! 101: } ! 102: } ! 103: *p = '\0'; ! 104: return(buf); ! 105: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.