Added upstream from http://ftp.icm.edu.pl/pub/loglan/
[loglan.git] / sources / f2c / cds.c
1 /****************************************************************
2 Copyright 1990 by AT&T Bell Laboratories and Bellcore.
3
4 Permission to use, copy, modify, and distribute this software
5 and its documentation for any purpose and without fee is hereby
6 granted, provided that the above copyright notice appear in all
7 copies and that both that the copyright notice and this
8 permission notice and warranty disclaimer appear in supporting
9 documentation, and that the names of AT&T Bell Laboratories or
10 Bellcore or any of their entities not be used in advertising or
11 publicity pertaining to distribution of the software without
12 specific, written prior permission.
13
14 AT&T and Bellcore disclaim all warranties with regard to this
15 software, including all implied warranties of merchantability
16 and fitness.  In no event shall AT&T or Bellcore be liable for
17 any special, indirect or consequential damages or any damages
18 whatsoever resulting from loss of use, data or profits, whether
19 in an action of contract, negligence or other tortious action,
20 arising out of or in connection with the use or performance of
21 this software.
22 ****************************************************************/
23
24 /* Put strings representing decimal floating-point numbers
25  * into canonical form: always have a decimal point or
26  * exponent field; if using an exponent field, have the
27  * number before it start with a digit and decimal point
28  * (if the number has more than one digit); only have an
29  * exponent field if it saves space.
30  *
31  * Arrange that the return value, rv, satisfies rv[0] == '-' || rv[-1] == '-' .
32  */
33
34 #include <stdio.h>
35
36  extern char *Alloc(), *mem(), *strcpy();
37
38  char *
39 cds(s, z0)
40  char *s, *z0;
41 {
42         int ea, esign, et, i, k, nd = 0, sign = 0, tz;
43         char c, *z;
44         char ebuf[24];
45         long ex = 0;
46         static char etype[256], *db;
47         static int dblen = 64;
48
49         if (!db) {
50                 etype['E'] = 1;
51                 etype['e'] = 1;
52                 etype['D'] = 1;
53                 etype['d'] = 1;
54                 etype['+'] = 2;
55                 etype['-'] = 3;
56                 db = Alloc(dblen);
57                 }
58
59         while((c = *s++) == '0');
60         if (c == '-')
61                 { sign = 1; c = *s++; }
62         else if (c == '+')
63                 c = *s++;
64         k = strlen(s) + 2;
65         if (k >= dblen) {
66                 do dblen <<= 1;
67                         while(k >= dblen);
68                 free(db);
69                 db = Alloc(dblen);
70                 }
71         if (etype[(unsigned char)c] >= 2)
72                 while(c == '0') c = *s++;
73         tz = 0;
74         while(c >= '0' && c <= '9') {
75                 if (c == '0')
76                         tz++;
77                 else {
78                         if (nd)
79                                 for(; tz; --tz)
80                                         db[nd++] = '0';
81                         else
82                                 tz = 0;
83                         db[nd++] = c;
84                         }
85                 c = *s++;
86                 }
87         ea = -tz;
88         if (c == '.') {
89                 while((c = *s++) >= '0' && c <= '9') {
90                         if (c == '0')
91                                 tz++;
92                         else {
93                                 if (tz) {
94                                         ea += tz;
95                                         if (nd)
96                                                 for(; tz; --tz)
97                                                         db[nd++] = '0';
98                                         else
99                                                 tz = 0;
100                                         }
101                                 db[nd++] = c;
102                                 ea++;
103                                 }
104                         }
105                 }
106         if (et = etype[(unsigned char)c]) {
107                 esign = et == 3;
108                 c = *s++;
109                 if (et == 1) {
110                         if(etype[(unsigned char)c] > 1) {
111                                 if (c == '-')
112                                         esign = 1;
113                                 c = *s++;
114                                 }
115                         }
116                 while(c >= '0' && c <= '9') {
117                         ex = 10*ex + (c - '0');
118                         c = *s++;
119                         }
120                 if (esign)
121                         ex = -ex;
122                 }
123         /* debug */ if (c)
124         /* debug*/      Fatal("unexpected character in cds");
125         ex -= ea;
126         if (!nd) {
127                 if (!z0)
128                         z0 = mem(4,0);
129                 strcpy(z0, "-0.");
130                 sign = 0;
131                 }
132         else if (ex > 2 || ex + nd < -2) {
133                 sprintf(ebuf, "%ld", ex + nd - 1);
134                 k = strlen(ebuf) + nd + 3;
135                 if (nd > 1)
136                         k++;
137                 if (!z0)
138                         z0 = mem(k,0);
139                 z = z0;
140                 *z++ = '-';
141                 *z++ = *db;
142                 if (nd > 1) {
143                         *z++ = '.';
144                         for(k = 1; k < nd; k++)
145                                 *z++ = db[k];
146                         }
147                 *z++ = 'e';
148                 strcpy(z, ebuf);
149                 }
150         else {
151                 k = (int)(ex + nd);
152                 i = nd + 3;
153                 if (k < 0)
154                         i -= k;
155                 else if (ex > 0)
156                         i += ex;
157                 if (!z0)
158                         z0 = mem(i,0);
159                 z = z0;
160                 *z++ = '-';
161                 if (ex >= 0) {
162                         for(k = 0; k < nd; k++)
163                                 *z++ = db[k];
164                         while(--ex >= 0)
165                                 *z++ = '0';
166                         *z++ = '.';
167                         }
168                 else {
169                         for(i = 0; i < k;)
170                                 *z++ = db[i++];
171                         *z++ = '.';
172                         while(++k <= 0)
173                                 *z++ = '0';
174                         while(i < nd)
175                                 *z++ = db[i++];
176                         }
177                 *z = 0;
178                 }
179         return sign ? z0 : z0+1;
180         }