1 /****************************************************************
3 The author of this software is David M. Gay.
5 Copyright (C) 1998 by Lucent Technologies
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
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
27 ****************************************************************/
29 /* Please send bug reports to
31 Bell Laboratories, Room 2C-463
33 Murray Hill, NJ 07974-0636
38 /* Test program for strtod and dtoa.
41 * number[: mode [ndigits]]
43 * #hex0 hex1[: mode [ndigits]]
44 * where number is a decimal floating-point number,
45 * hex0 is a string of Hex digits for the most significant
46 * word of the number, hex1 is a similar string for the other
47 * (least significant) word, and mode and ndigits are
63 extern "C" double atof(const char*);
65 extern double atof ANSI((char*));
69 #define word0(x) ((ULong *)&x)[1]
70 #define word1(x) ((ULong *)&x)[0]
72 #define word0(x) ((ULong *)&x)[0]
73 #define word1(x) ((ULong *)&x)[1]
78 extern "C" char *dtoa(double, int, int, int*, int*, char **);
80 extern char *dtoa ANSI((double, int, int, int*, int*, char **));
85 g_fmt(b, x) char *b; double x;
87 g_fmt(char *b, double x)
91 int decpt, i, j, k, sign;
98 s = dtoa(x, 0, 0, &decpt, &sign, &se);
101 if (decpt == 9999) /* Infinity or Nan */ {
105 if (decpt <= -4 || decpt > se - s + 5) {
113 /* sprintf(b, "%+.2d", decpt - 1); */
120 for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10){};
131 else if (decpt <= 0) {
133 for(; decpt < 0; decpt++)
140 if (--decpt == 0 && *s)
143 for(; decpt > 0; decpt--)
153 perror("\nerrno strtod");
157 #define U (unsigned long)
171 s = dtoa(d, 0, 0, &decpt, &sign, &se);
172 sprintf(buf, "%s.%se%d", sign ? "-" : "", s, decpt);
174 d1 = strtod(buf, (char **)0);
178 printf("sent d = %.17g = 0x%lx %lx, buf = %s\n",
179 d, U word0(d), U word1(d), buf);
180 printf("got d1 = %.17g = 0x%lx %lx\n",
181 d1, U word0(d1), U word1(d1));
186 char buf[2048], buf1[32];
190 int mode = 0, ndigits = 17;
196 while(fgets(buf, sizeof(buf), stdin)) {
201 printf("Input: %s", buf);
205 sscanf(buf+1, "%lx %lx:%d %d", &x, &y, &mode, &ndigits);
208 fmt = "Output: d =\n%.17g = 0x%lx %lx\n";
214 sscanf(se+1,"%d %d", &mode, &ndigits);
216 fmt = "Output: d =\n%.17g = 0x%lx %lx, se = %s";
220 printf(fmt, d, U word0(d), U word1(d), se);
222 printf("\tg_fmt gives \"%s\"\n", buf1);
223 if (*buf != '#' && d != d1)
224 printf("atof gives\n\
225 d1 = %.17g = 0x%lx %lx\nversus\n\
226 d = %.17g = 0x%lx %lx\n", d1, U word0(d1), U word1(d1),
227 d, U word0(d), U word1(d));
229 s = dtoa(d, mode, ndigits, &decpt, &sign, &se);
230 printf("\tdtoa(mode = %d, ndigits = %d):\n", mode, ndigits);
231 printf("\tdtoa returns sign = %d, decpt = %d, %d digits:\n%s\n",
232 sign, decpt, se-s, s);
235 && (word0(d) & 0x7ff00000) != 0x7ff00000) {
237 z = x << 16 | x >> 16;
239 z = z << 16 | z >> 16;
244 printf("\tnextafter(d,+Inf) = %.17g = 0x%lx %lx:\n",
245 d, U word0(d), U word1(d));
247 printf("\tg_fmt gives \"%s\"\n", buf1);
248 s = dtoa(d, mode, ndigits, &decpt, &sign, &se);
250 "\tdtoa returns sign = %d, decpt = %d, %d digits:\n%s\n",
251 sign, decpt, se-s, s);
256 z = x << 16 | x >> 16;
258 z = z << 16 | z >> 16;
263 printf("\tnextafter(d,-Inf) = %.17g = 0x%lx %lx:\n",
264 d, U word0(d), U word1(d));
266 printf("\tg_fmt gives \"%s\"\n", buf1);
267 s = dtoa(d, mode, ndigits, &decpt, &sign, &se);
269 "\tdtoa returns sign = %d, decpt = %d, %d digits:\n%s\n",
270 sign, decpt, se-s, s);