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 David M. Gay (dmg at acm dot org,
30 * with " at " changed at "@" and " dot " changed to "."). */
32 /* Test program for strtod and dtoa.
35 * number[: mode [ndigits]]
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
57 extern "C" double atof(const char*);
59 extern double atof
ANSI((char*));
63 #define word0(x) ((ULong *)&x)[1]
64 #define word1(x) ((ULong *)&x)[0]
66 #define word0(x) ((ULong *)&x)[0]
67 #define word1(x) ((ULong *)&x)[1]
72 extern "C" char *dtoa(double, int, int, int*, int*, char **);
74 extern char *dtoa
ANSI((double, int, int, int*, int*, char **));
79 g_fmt(b
, x
) char *b
; double x
;
81 g_fmt(char *b
, double x
)
85 int decpt
, i
, j
, k
, sign
;
92 s
= dtoa(x
, 0, 0, &decpt
, &sign
, &se
);
95 if (decpt
== 9999) /* Infinity or Nan */ {
99 if (decpt
<= -4 || decpt
> se
- s
+ 5) {
107 /* sprintf(b, "%+.2d", decpt - 1); */
114 for(j
= 2, k
= 10; 10*k
<= decpt
; j
++, k
*= 10){};
125 else if (decpt
<= 0) {
127 for(; decpt
< 0; decpt
++)
134 if (--decpt
== 0 && *s
)
137 for(; decpt
> 0; decpt
--)
147 perror("\nerrno strtod");
151 #define U (unsigned long)
165 s
= dtoa(d
, 0, 0, &decpt
, &sign
, &se
);
166 sprintf(buf
, "%s%s%se%d", sign
? "-" : "",
167 decpt
== 9999 ? "" : ".", s
, decpt
);
169 d1
= strtod(buf
, (char **)0);
173 printf("sent d = %.17g = 0x%lx %lx, buf = %s\n",
174 d
, U
word0(d
), U
word1(d
), buf
);
175 printf("got d1 = %.17g = 0x%lx %lx\n",
176 d1
, U
word0(d1
), U
word1(d1
));
182 char buf
[2048], buf1
[32];
183 char *fmt
, *s
, *s1
, *se
;
186 int mode
= 0, ndigits
= 17;
192 while(fgets(buf
, sizeof(buf
), stdin
)) {
197 printf("Input: %s", buf
);
201 /* sscanf(buf+1, "%lx %lx:%d %d", &x, &y, &mode, &ndigits); */
202 x
= (ULong
)strtoul(s1
= buf
+1, &se
, 16);
204 y
= (ULong
)strtoul(s1
= se
, &se
, 16);
206 sscanf(se
, ":%d %d", &mode
, &ndigits
);
210 fmt
= "Output: d =\n%.17g = 0x%lx %lx\n";
216 sscanf(se
+1,"%d %d", &mode
, &ndigits
);
218 fmt
= "Output: d =\n%.17g = 0x%lx %lx, se = %s";
222 printf(fmt
, d
, U
word0(d
), U
word1(d
), se
);
224 printf("\tg_fmt gives \"%s\"\n", buf1
);
225 if (*buf
!= '#' && d
!= d1
)
226 printf("atof gives\n\
227 d1 = %.17g = 0x%lx %lx\nversus\n\
228 d = %.17g = 0x%lx %lx\n", d1
, U
word0(d1
), U
word1(d1
),
229 d
, U
word0(d
), U
word1(d
));
231 s
= dtoa(d
, mode
, ndigits
, &decpt
, &sign
, &se
);
232 printf("\tdtoa(mode = %d, ndigits = %d):\n", mode
, ndigits
);
233 printf("\tdtoa returns sign = %d, decpt = %d, %d digits:\n%s\n",
234 sign
, decpt
, se
-s
, s
);
237 && (word0(d
) & 0x7ff00000) != 0x7ff00000) {
239 z
= x
<< 16 | x
>> 16;
241 z
= z
<< 16 | z
>> 16;
246 printf("\tnextafter(d,+Inf) = %.17g = 0x%lx %lx:\n",
247 d
, U
word0(d
), U
word1(d
));
249 printf("\tg_fmt gives \"%s\"\n", buf1
);
250 s
= dtoa(d
, mode
, ndigits
, &decpt
, &sign
, &se
);
252 "\tdtoa returns sign = %d, decpt = %d, %d digits:\n%s\n",
253 sign
, decpt
, se
-s
, s
);
258 z
= x
<< 16 | x
>> 16;
260 z
= z
<< 16 | z
>> 16;
265 printf("\tnextafter(d,-Inf) = %.17g = 0x%lx %lx:\n",
266 d
, U
word0(d
), U
word1(d
));
268 printf("\tg_fmt gives \"%s\"\n", buf1
);
269 s
= dtoa(d
, mode
, ndigits
, &decpt
, &sign
, &se
);
271 "\tdtoa returns sign = %d, decpt = %d, %d digits:\n%s\n",
272 sign
, decpt
, se
-s
, s
);