Debian Codegraph @

runtime/gdtoa/test/dt.c

raw symbols SCIP
1 /****************************************************************
2
3 The author of this software is David M. Gay.
4
5 Copyright (C) 1998 by Lucent Technologies
6 All Rights Reserved
7
8 Permission to use, copy, modify, and distribute this software and
9 its documentation for any purpose and without fee is hereby
10 granted, provided that the above copyright notice appear in all
11 copies and that both that the copyright notice and this
12 permission notice and warranty disclaimer appear in supporting
13 documentation, and that the name of Lucent or any of its entities
14 not be used in advertising or publicity pertaining to
15 distribution of the software without specific, written prior
16 permission.
17
18 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25 THIS SOFTWARE.
26
27 ****************************************************************/
28
29 /* Please send bug reports to David M. Gay (dmg at acm dot org,
30 * with " at " changed at "@" and " dot " changed to "."). */
31
32 /* Test program for strtod and dtoa.
33 *
34 * Inputs (on stdin):
35 * number[: mode [ndigits]]
36 * or
37 * #hex0 hex1[: mode [ndigits]]
38 * where number is a decimal floating-point number,
39 * hex0 is a string of Hex digits for the most significant
40 * word of the number, hex1 is a similar string for the other
41 * (least significant) word, and mode and ndigits are
42 * parameters to dtoa.
43 */
44
45 #include <stdio.h>
46 #include "gdtoa.h"
47 int STRTOD_DIGLIM = 24;
48 #ifdef KR_headers
49 #define Void /*void*/
50 #else
51 #define Void void
52 #endif
53
54 #ifdef __STDC__
55 #include <stdlib.h>
56 #else
57 #ifdef __cplusplus
58 extern "C" double atof(const char*);
59 #else
60 extern double atof ANSI((char*));
61 #endif
62 #endif
63
64 typedef union { double d; ULong L[2]; } U;
65
66 #ifdef IEEE_8087
67 #define word0(x) (x)->L[1]
68 #define word1(x) (x)->L[0]
69 #else
70 #define word0(x) (x)->L[0]
71 #define word1(x) (x)->L[1]
72 #endif
73 #define dval(x) (x)->d
74
75 #include "errno.h"
76
77 #ifdef __cplusplus
78 extern "C" char *dtoa(double, int, int, int*, int*, char **);
79 #else
80 extern char *dtoa ANSI((double, int, int, int*, int*, char **));
81 #endif
82
83 static void
84 #ifdef KR_headers
85 g_fmt(b, x) char *b; double x;
86 #else
87 g_fmt(char *b, double x)
88 #endif
89 {
90 char *s, *se;
91 int decpt, i, j, k, sign;
92
93 if (!x) {
94 *b++ = '0';
95 *b = 0;
96 return;
97 }
98 s = dtoa(x, 0, 0, &decpt, &sign, &se);
99 if (sign)
100 *b++ = '-';
101 if (decpt == 9999) /* Infinity or Nan */ {
102 while((*b++ = *s++));
103 return;
104 }
105 if (decpt <= -4 || decpt > se - s + 5) {
106 *b++ = *s++;
107 if (*s) {
108 *b++ = '.';
109 while((*b = *s++))
110 b++;
111 }
112 *b++ = 'e';
113 /* sprintf(b, "%+.2d", decpt - 1); */
114 if (--decpt < 0) {
115 *b++ = '-';
116 decpt = -decpt;
117 }
118 else
119 *b++ = '+';
120 for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10){};
121 for(;;) {
122 i = decpt / k;
123 *b++ = i + '0';
124 if (--j <= 0)
125 break;
126 decpt -= i*k;
127 decpt *= 10;
128 }
129 *b = 0;
130 }
131 else if (decpt <= 0) {
132 *b++ = '.';
133 for(; decpt < 0; decpt++)
134 *b++ = '0';
135 while((*b++ = *s++));
136 }
137 else {
138 while((*b = *s++)) {
139 b++;
140 if (--decpt == 0 && *s)
141 *b++ = '.';
142 }
143 for(; decpt > 0; decpt--)
144 *b++ = '0';
145 *b = 0;
146 }
147 }
148
149 static void
150 baderrno(Void)
151 {
152 fflush(stdout);
153 perror("\nerrno strtod");
154 fflush(stderr);
155 }
156
157 #define UL (unsigned long)
158
159 static void
160 #ifdef KR_headers
161 check(d) U *d;
162 #else
163 check(U *d)
164 #endif
165 {
166 char buf[64];
167 int decpt, sign;
168 char *s, *se;
169 U d1;
170
171 s = dtoa(dval(d), 0, 0, &decpt, &sign, &se);
172 sprintf(buf, "%s%s%se%d", sign ? "-" : "",
173 decpt == 9999 ? "" : ".", s, decpt);
174 errno = 0;
175 dval(&d1) = strtod(buf, (char **)0);
176 if (errno)
177 baderrno();
178 if (dval(d) != dval(&d1)) {
179 printf("sent d = %.17g = 0x%lx %lx, buf = %s\n",
180 dval(d), UL word0(d), UL word1(d), buf);
181 printf("got d1 = %.17g = 0x%lx %lx\n",
182 dval(&d1), UL word0(&d1), UL word1(&d1));
183 }
184 }
185
186 #ifdef USE_fpinit
187 extern void fpinit_ASL();
188 /* fpinit_ASL() appears in the AMPL/solver interface library and with */
189 /* Intel 80x87-style processing, sets the rounding precision to 53 bits. */
190 #else
191 #define fpinit_ASL() /*nothing*/
192 #endif
193
194 int
195 main(Void)
196 {
197 U d, d1;
198 char buf[2048], buf1[32];
199 char *fmt, *s, *s1, *se;
200 int decpt, sign;
201 int mode = 0, ndigits = 17;
202 ULong x, y;
203 #ifdef VAX
204 ULong z;
205 #endif
206
207 fpinit_ASL();
208 d.d = 0.;
209 while(fgets(buf, sizeof(buf), stdin)) {
210 if (*buf == '*') {
211 printf("%s", buf);
212 continue;
213 }
214 printf("Input: %s", buf);
215 if (*buf == '#') {
216 x = word0(&d);
217 y = word1(&d);
218 /* sscanf(buf+1, "%lx %lx:%d %d", &x, &y, &mode, &ndigits); */
219 x = (ULong)strtoul(s1 = buf+1, &se, 16);
220 if (se > s1) {
221 y = (ULong)strtoul(s1 = se, &se, 16);
222 if (se > s1)
223 sscanf(se, ":%d %d", &mode, &ndigits);
224 }
225 word0(&d) = x;
226 word1(&d) = y;
227 fmt = "Output: d =\n%.17g = 0x%lx %lx\n";
228 }
229 else if (*buf == '*') {
230 x = strtoul(buf,&s,10);
231 if (!*s && x > 18)
232 STRTOD_DIGLIM = (int)x;
233 printf("STRTOD_DIGLIM = %lu\n", UL x);
234 continue;
235 }
236 else {
237 errno = 0;
238 dval(&d) = strtod(buf,&se);
239 if (*se == ':')
240 sscanf(se+1,"%d %d", &mode, &ndigits);
241 dval(&d1) = atof(buf);
242 fmt = "Output: d =\n%.17g = 0x%lx %lx, se = %s";
243 if (errno)
244 baderrno();
245 }
246 printf(fmt, dval(&d), UL word0(&d), UL word1(&d), se);
247 g_fmt(buf1, dval(&d));
248 printf("\tg_fmt gives \"%s\"\n", buf1);
249 if (*buf != '#' && dval(&d) != dval(&d1))
250 printf("atof gives\n\
251 d1 = %.17g = 0x%lx %lx\nversus\n\
252 d = %.17g = 0x%lx %lx\n", dval(&d1), UL word0(&d1), UL word1(&d1),
253 dval(&d), UL word0(&d), UL word1(&d));
254 check(&d);
255 s = dtoa(dval(&d), mode, ndigits, &decpt, &sign, &se);
256 printf("\tdtoa(mode = %d, ndigits = %d):\n", mode, ndigits);
257 printf("\tdtoa returns sign = %d, decpt = %d, %d digits:\n%s\n",
258 sign, decpt, (int)(se-s), s);
259 x = word1(&d);
260 if (x != 0xffffffff
261 && (word0(&d) & 0x7ff00000) != 0x7ff00000) {
262 #ifdef VAX
263 z = x << 16 | x >> 16;
264 z++;
265 z = z << 16 | z >> 16;
266 word1(&d) = z;
267 #else
268 word1(&d) = x + 1;
269 #endif
270 printf("\tnextafter(d,+Inf) = %.17g = 0x%lx %lx:\n",
271 dval(&d), UL word0(&d), UL word1(&d));
272 g_fmt(buf1, dval(&d));
273 printf("\tg_fmt gives \"%s\"\n", buf1);
274 s = dtoa(dval(&d), mode, ndigits, &decpt, &sign, &se);
275 printf(
276 "\tdtoa returns sign = %d, decpt = %d, %d digits:\n%s\n",
277 sign, decpt, (int)(se-s), s);
278 check(&d);
279 }
280 if (x) {
281 #ifdef VAX
282 z = x << 16 | x >> 16;
283 z--;
284 z = z << 16 | z >> 16;
285 word1(&d) = z;
286 #else
287 word1(&d) = x - 1;
288 #endif
289 printf("\tnextafter(d,-Inf) = %.17g = 0x%lx %lx:\n",
290 dval(&d), UL word0(&d), UL word1(&d));
291 g_fmt(buf1, dval(&d));
292 printf("\tg_fmt gives \"%s\"\n", buf1);
293 s = dtoa(dval(&d), mode, ndigits, &decpt, &sign, &se);
294 printf(
295 "\tdtoa returns sign = %d, decpt = %d, %d digits:\n%s\n",
296 sign, decpt, (int)(se-s), s);
297 check(&d);
298 }
299 }
300 return 0;
301 }