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