OSDN Git Service

Fix no pic
[uclinux-h8/uClinux-dist.git] / lib / libm / ftoa.c
1
2 #define PSH(X) (*(st++)=(X))
3
4 #define PLUS 1
5 #define SPACE 2
6
7 #define fabs(x) ((x)<0 ? (-x) : (x))
8
9 /* FIXME: This file contains roundoff error */
10
11 char * ftoa(char *st, float f, int flags)
12 {
13 #if 0
14  int i;
15  float frac;
16
17  i = (int) (f*1000.0);
18  frac = fabs(f - i);
19  sprintf(st, "%d+e4", i);
20  /*if (frac > 1e-4)
21    strcat(st, ".");
22  while (frac > 1e-4) {
23    frac *= 10;
24    sprintf(st+strlen(st), "%d", (int)frac);
25  }*/
26 #endif
27  
28 #if 1
29   int i;
30   int z;
31   int exp = 0;
32
33   if (f < 0) {
34     PSH('-');
35     f = -f;
36   } else {
37     if (flags & PLUS) PSH('+');
38     if (flags & SPACE) PSH(' ');
39   }
40
41   if (f) {
42     while (f < 1) {
43       f *=10;
44       exp--;
45     }
46
47     while (f >= 10) {
48       f /=10;
49       exp++;
50     }
51   }
52
53   while ((exp > 0) && (exp < 7)) {
54           PSH('0'+f);
55           z = f;
56           f -= z;
57           f *= 10;
58         exp--;
59   }
60
61   PSH('0'+f);
62   z = f;
63   f -= z;
64   f *= 10;
65
66   PSH('.');
67
68   for (i=0;i<2;i++) {
69     PSH('0'+f);
70     z = f;
71     f -= z;
72     f *= 10;
73   }
74   
75   if (exp != 0) {
76
77           PSH('e');
78           if (exp < 0) {
79             PSH('-');
80             exp = -exp;
81           } else {
82             PSH('+');
83           }
84
85           PSH('0'+exp/10);
86           exp -= (exp/10) * 10;
87           PSH('0'+exp);
88   
89
90  }
91
92   PSH(0);
93
94
95   return st;
96 #endif
97 }