1 /****************************************************************
3 The author of this software is David M. Gay.
5 Copyright (C) 1998-2001 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 g_ddfmt, strtoIdd, strtopdd, and strtordd.
38 * #hex0 hex1 hex2 hex3
40 * rounding_mode values:
43 * 2 = toward +Infinity
44 * 3 = toward -Infinity
46 * where number is a decimal floating-point number,
47 * hex0 is a string of <= 8 Hex digits for the most significant
48 * word of the number, hex1 is a similar string for the next
49 * word, etc., and ndig is a parameters to g_ddfmt.
56 extern int getround
ANSI((int,char*));
58 static char ibuf
[2048], obuf
[1024];
60 #define U (unsigned long)
64 dprint(what
, d
) char *what
; double d
;
66 dprint(char *what
, double d
)
70 union { double d
; ULong L
[2]; } u
;
73 g_dfmt(buf
,&d
,0,sizeof(buf
));
74 printf("%s = %s = #%lx %lx\n", what
, buf
, U u
.L
[_0
], U u
.L
[_1
]);
80 char *s
, *s1
, *se
, *se1
;
81 int dItry
, i
, j
, r
= 1, ndig
= 0;
84 union { double dd
[2]; ULong L
[4]; } u
;
86 while( (s
= fgets(ibuf
, sizeof(ibuf
), stdin
)) !=0) {
97 if (i
<= ' ' || (i
>= '0' && i
<= '9')) {
107 sscanf(s
+1, "%lx %lx %lx %lx", &LL
[0], &LL
[1],
113 printf("\nInput: %s", ibuf
);
114 printf(" --> f = #%lx %lx %lx %lx\n",
115 LL
[0],LL
[1],LL
[2],LL
[3]);
118 printf("\nInput: %s", ibuf
);
119 for(s1
= s
; *s1
> ' '; s1
++){};
120 while(*s1
<= ' ' && *s1
) s1
++;
123 i
= strtordd(ibuf
, &se
, r
, u
.dd
);
125 j
= strtopdd(ibuf
, &se1
, ddI
);
126 if (i
!= j
|| u
.dd
[0] != ddI
[0]
127 || u
.dd
[1] != ddI
[1] || se
!= se1
)
128 printf("***strtopdd and strtordd disagree!!\n:");
130 printf("strtopdd consumes %d bytes and returns %d\n",
134 u
.dd
[0] = strtod(s
, &se
);
135 u
.dd
[1] = strtod(se
, &se
);
138 dprint("dd[0]", u
.dd
[0]);
139 dprint("dd[1]", u
.dd
[1]);
140 se
= g_ddfmt(obuf
, u
.dd
, ndig
, sizeof(obuf
));
141 printf("g_ddfmt(%d) gives %d bytes: \"%s\"\n\n",
142 ndig
, (int)(se
-obuf
), se
? obuf
: "<null>");
145 printf("strtoIdd returns %d,", strtoIdd(ibuf
, &se
, ddI
,&ddI
[2]));
146 printf(" consuming %d bytes.\n", (int)(se
-ibuf
));
147 if (ddI
[0] == ddI
[2] && ddI
[1] == ddI
[3]) {
148 if (ddI
[0] == u
.dd
[0] && ddI
[1] == u
.dd
[1])
149 printf("ddI[0] == ddI[1] == strtopdd\n");
151 printf("ddI[0] == ddI[1] = #%lx %lx + %lx %lx\n= %.17g + %17.g\n",
154 U ((ULong
*)ddI
)[2+_0
],
155 U ((ULong
*)ddI
)[2+_1
],
159 printf("ddI[0] = #%lx %lx + %lx %lx\n= %.17g + %.17g\n",
160 U ((ULong
*)ddI
)[_0
], U ((ULong
*)ddI
)[_1
],
161 U ((ULong
*)ddI
)[2+_0
], U ((ULong
*)ddI
)[2+_1
],
163 printf("ddI[1] = #%lx %lx + %lx %lx\n= %.17g + %.17g\n",
164 U ((ULong
*)ddI
)[4+_0
], U ((ULong
*)ddI
)[4+_1
],
165 U ((ULong
*)ddI
)[6+_0
], U ((ULong
*)ddI
)[6+_1
],
167 if (ddI
[0] == u
.dd
[0] && ddI
[1] == u
.dd
[1])
168 printf("ddI[0] == strtod\n");
169 else if (ddI
[2] == u
.dd
[0] && ddI
[3] == u
.dd
[1])
170 printf("ddI[1] == strtod\n");
172 printf("**** Both differ from strtopdd ****\n");