1 | /****************************************************************
|
---|
2 |
|
---|
3 | The author of this software is David M. Gay.
|
---|
4 |
|
---|
5 | Copyright (C) 1998-2001 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
|
---|
30 | David M. Gay
|
---|
31 | Bell Laboratories, Room 2C-463
|
---|
32 | 600 Mountain Avenue
|
---|
33 | Murray Hill, NJ 07974-0636
|
---|
34 | U.S.A.
|
---|
35 | dmg@bell-labs.com
|
---|
36 | */
|
---|
37 |
|
---|
38 | /* Test program for g_ddfmt, strtoIdd, strtopdd, and strtordd.
|
---|
39 | *
|
---|
40 | * Inputs (on stdin):
|
---|
41 | * r rounding_mode
|
---|
42 | * n ndig
|
---|
43 | * number
|
---|
44 | * #hex0 hex1 hex2 hex3
|
---|
45 | *
|
---|
46 | * rounding_mode values:
|
---|
47 | * 0 = toward zero
|
---|
48 | * 1 = nearest
|
---|
49 | * 2 = toward +Infinity
|
---|
50 | * 3 = toward -Infinity
|
---|
51 | *
|
---|
52 | * where number is a decimal floating-point number,
|
---|
53 | * hex0 is a string of <= 8 Hex digits for the most significant
|
---|
54 | * word of the number, hex1 is a similar string for the next
|
---|
55 | * word, etc., and ndig is a parameters to g_ddfmt.
|
---|
56 | */
|
---|
57 |
|
---|
58 | #include "gdtoaimp.h"
|
---|
59 | #include <stdio.h>
|
---|
60 | #include <stdlib.h>
|
---|
61 |
|
---|
62 | extern int getround ANSI((int,char*));
|
---|
63 |
|
---|
64 | static char ibuf[2048], obuf[1024];
|
---|
65 |
|
---|
66 | #define U (unsigned long)
|
---|
67 |
|
---|
68 | static void
|
---|
69 | #ifdef KR_headers
|
---|
70 | dprint(what, d) char *what; double d;
|
---|
71 | #else
|
---|
72 | dprint(char *what, double d)
|
---|
73 | #endif
|
---|
74 | {
|
---|
75 | char buf[32];
|
---|
76 | ULong *L = (ULong*)&d;
|
---|
77 |
|
---|
78 | g_dfmt(buf,&d,0,sizeof(buf));
|
---|
79 | printf("%s = %s = #%lx %lx\n", what, buf, U L[_0], U L[_1]);
|
---|
80 | }
|
---|
81 |
|
---|
82 | int
|
---|
83 | main(Void)
|
---|
84 | {
|
---|
85 | ULong *L;
|
---|
86 | char *s, *s1, *se, *se1;
|
---|
87 | int dItry, i, j, r = 1, ndig = 0;
|
---|
88 | double dd[2], ddI[4];
|
---|
89 | long LL[4];
|
---|
90 |
|
---|
91 | L = (ULong*)&dd[0];
|
---|
92 | while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) {
|
---|
93 | while(*s <= ' ')
|
---|
94 | if (!*s++)
|
---|
95 | continue;
|
---|
96 | dItry = 0;
|
---|
97 | switch(*s) {
|
---|
98 | case 'r':
|
---|
99 | r = getround(r, s);
|
---|
100 | continue;
|
---|
101 | case 'n':
|
---|
102 | i = s[1];
|
---|
103 | if (i <= ' ' || i >= '0' && i <= '9') {
|
---|
104 | ndig = atoi(s+1);
|
---|
105 | continue;
|
---|
106 | }
|
---|
107 | break; /* nan? */
|
---|
108 | case '#':
|
---|
109 | LL[0] = L[_0];
|
---|
110 | LL[1] = L[_1];
|
---|
111 | LL[2] = L[2+_0];
|
---|
112 | LL[3] = L[2+_1];
|
---|
113 | sscanf(s+1, "%lx %lx %lx %lx", &LL[0], &LL[1],
|
---|
114 | &LL[2], &LL[3]);
|
---|
115 | L[_0] = LL[0];
|
---|
116 | L[_1] = LL[1];
|
---|
117 | L[2+_0] = LL[2];
|
---|
118 | L[2+_1] = LL[3];
|
---|
119 | printf("\nInput: %s", ibuf);
|
---|
120 | printf(" --> f = #%lx %lx %lx %lx\n",
|
---|
121 | LL[0],LL[1],LL[2],LL[3]);
|
---|
122 | goto fmt_test;
|
---|
123 | }
|
---|
124 | printf("\nInput: %s", ibuf);
|
---|
125 | for(s1 = s; *s1 > ' '; s1++){};
|
---|
126 | while(*s1 <= ' ' && *s1) s1++;
|
---|
127 | if (!*s1) {
|
---|
128 | dItry = 1;
|
---|
129 | i = strtordd(ibuf, &se, r, dd);
|
---|
130 | if (r == 1) {
|
---|
131 | j = strtopdd(ibuf, &se1, ddI);
|
---|
132 | if (i != j || dd[0] != ddI[0]
|
---|
133 | || dd[1] != ddI[1] || se != se1)
|
---|
134 | printf("***strtopdd and strtordd disagree!!\n:");
|
---|
135 | }
|
---|
136 | printf("strtopdd consumes %d bytes and returns %d\n",
|
---|
137 | (int)(se-ibuf), i);
|
---|
138 | }
|
---|
139 | else {
|
---|
140 | dd[0] = strtod(s, &se);
|
---|
141 | dd[1] = strtod(se, &se);
|
---|
142 | }
|
---|
143 | fmt_test:
|
---|
144 | dprint("dd[0]", dd[0]);
|
---|
145 | dprint("dd[1]", dd[1]);
|
---|
146 | se = g_ddfmt(obuf, dd, ndig, sizeof(obuf));
|
---|
147 | printf("g_ddfmt(%d) gives %d bytes: \"%s\"\n\n",
|
---|
148 | ndig, (int)(se-obuf), se ? obuf : "<null>");
|
---|
149 | if (!dItry)
|
---|
150 | continue;
|
---|
151 | printf("strtoIdd returns %d,", strtoIdd(ibuf, &se, ddI,&ddI[2]));
|
---|
152 | printf(" consuming %d bytes.\n", (int)(se-ibuf));
|
---|
153 | if (ddI[0] == ddI[2] && ddI[1] == ddI[3]) {
|
---|
154 | if (ddI[0] == dd[0] && ddI[1] == dd[1])
|
---|
155 | printf("ddI[0] == ddI[1] == strtopdd\n");
|
---|
156 | else
|
---|
157 | printf("ddI[0] == ddI[1] = #%lx %lx + %lx %lx\n= %.17g + %17.g\n",
|
---|
158 | U ((ULong*)ddI)[_0],
|
---|
159 | U ((ULong*)ddI)[_1],
|
---|
160 | U ((ULong*)ddI)[2+_0],
|
---|
161 | U ((ULong*)ddI)[2+_1],
|
---|
162 | ddI[0], ddI[1]);
|
---|
163 | }
|
---|
164 | else {
|
---|
165 | printf("ddI[0] = #%lx %lx + %lx %lx\n= %.17g + %.17g\n",
|
---|
166 | U ((ULong*)ddI)[_0], U ((ULong*)ddI)[_1],
|
---|
167 | U ((ULong*)ddI)[2+_0], U ((ULong*)ddI)[2+_1],
|
---|
168 | ddI[0], ddI[1]);
|
---|
169 | printf("ddI[1] = #%lx %lx + %lx %lx\n= %.17g + %.17g\n",
|
---|
170 | U ((ULong*)ddI)[4+_0], U ((ULong*)ddI)[4+_1],
|
---|
171 | U ((ULong*)ddI)[6+_0], U ((ULong*)ddI)[6+_1],
|
---|
172 | ddI[2], ddI[3]);
|
---|
173 | if (ddI[0] == dd[0] && ddI[1] == dd[1])
|
---|
174 | printf("ddI[0] == strtod\n");
|
---|
175 | else if (ddI[2] == dd[0] && ddI[3] == dd[1])
|
---|
176 | printf("ddI[1] == strtod\n");
|
---|
177 | else
|
---|
178 | printf("**** Both differ from strtopdd ****\n");
|
---|
179 | }
|
---|
180 | printf("\n");
|
---|
181 | }
|
---|
182 | return 0;
|
---|
183 | }
|
---|