1: ftoa (x, str, prec, format)
2: float x;
3: char *str;
4: {
5: /* converts a floating point number to an ascii string */
6: /* x is stored into str, which should be at least 30 chars long */
7: int ie, i, k, ndig, fstyle;
8: double y;
9: if (nargs() != 7)
10: IEHzap("ftoa ");
11: ndig = ( prec<=0) ? 7 : (prec > 22 ? 23 : prec+1);
12: if (format == 'f' || format == 'F')
13: fstyle = 1;
14: else
15: fstyle = 0;
16: /* print in e format unless last arg is 'f' */
17: ie = 0;
18: /* if x negative, write minus and reverse */
19: if ( x < 0)
20: {
21: *str++ = '-';
22: x = -x;
23: }
24:
25: /* put x in range 1 <= x < 10 */
26: if (x > 0.0) while (x < 1.0)
27: {
28: x =* 10.0;
29: ie--;
30: }
31: while (x >= 10.0)
32: {
33: x = x/10.0;
34: ie++;
35: }
36:
37: /* in f format, number of digits is related to size */
38: if (fstyle) ndig =+ ie;
39:
40: /* round. x is between 1 and 10 and ndig will be printed to
41: right of decimal point so rounding is ... */
42: for (y = i = 1; i < ndig; i++)
43: y = y/10.;
44: x =+ y/2.;
45: if (x >= 10.0) {x = 1.0; ie++;} /* repair rounding disasters */
46: /* now loop. put out a digit (obtain by multiplying by
47: 10, truncating, subtracting) until enough digits out */
48: /* if fstyle, and leading zeros, they go out special */
49: if (fstyle && ie<0)
50: {
51: *str++ = '0'; *str++ = '.';
52: if (ndig < 0) ie = ie-ndig; /* limit zeros if underflow */
53: for (i = -1; i > ie; i--)
54: *str++ = '0';
55: }
56: for (i=0; i < ndig; i++)
57: {
58: k = x;
59: *str++ = k + '0';
60: if (i == (fstyle ? ie : 0)) /* where is decimal point */
61: *str++ = '.';
62: x =- (y=k);
63: x =* 10.0;
64: }
65:
66: /* now, in estyle, put out exponent if not zero */
67: if (!fstyle && ie != 0)
68: {
69: *str++ = 'E';
70: if (ie < 0)
71: {
72: ie = -ie;
73: *str++ = '-';
74: }
75: for (k=100; k > ie; k =/10);
76: for (; k > 0; k =/10)
77: {
78: *str++ = ie/k + '0';
79: ie = ie%k;
80: }
81: }
82: *str = '\0';
83: return;
84: }
Defined functions
ftoa
defined in line
1; used 1 times