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_ffmt, strtof, strtoIf, strtopf, and strtorf.
40 * rounding_mode values:
43 * 2 = toward +Infinity
44 * 3 = toward -Infinity
46 * where number is a decimal floating-point number,
47 * hex is a string of <= 8 Hex digits for the internal representation
48 * of the number, and ndig is a parameters to g_ffmt.
55 extern int getround
ANSI((int,char*));
57 static char ibuf
[2048], obuf
[1024];
59 #define U (unsigned long)
65 int dItry
, i
, i1
, ndig
= 0, r
= 1;
67 union { float f
; ULong L
[1]; } u
;
69 while( (s
= fgets(ibuf
, sizeof(ibuf
), stdin
)) !=0) {
80 if (i
<= ' ' || (i
>= '0' && i
<= '9')) {
86 /* sscanf(s+1, "%lx", &u.L[0]); */
87 u
.L
[0] = (ULong
)strtoul(s
+1, &se
, 16);
88 printf("\nInput: %s", ibuf
);
89 printf(" --> f = #%lx\n", U u
.L
[0]);
93 printf("\nInput: %s", ibuf
);
94 i
= strtorf(ibuf
, &se
, r
, &u
.f
);
96 if (u
.f
!= (i1
= strtopf(ibuf
, &se1
, &f1
), f1
)
97 || se
!= se1
|| i
!= i1
) {
98 printf("***strtopf and strtorf disagree!!\n");
100 printf("\tf1 = %g\n", (double)f1
);
102 printf("\ti = %d but i1 = %d\n", i
, i1
);
104 printf("se - se1 = %d\n", (int)(se
-se1
));
106 if (u
.f
!= strtof(ibuf
, &se1
) || se
!= se1
)
107 printf("***strtof and strtorf disagree!\n");
109 printf("strtof consumes %d bytes and returns %.8g = #%lx\n",
110 (int)(se
-ibuf
), u
.f
, U u
.L
[0]);
112 se
= g_ffmt(obuf
, &u
.f
, ndig
, sizeof(obuf
));
113 printf("g_ffmt(%d) gives %d bytes: \"%s\"\n\n",
114 ndig
, (int)(se
-obuf
), se
? obuf
: "<null>");
117 printf("strtoIf returns %d,", strtoIf(ibuf
, &se
, fI
, &fI
[1]));
118 printf(" consuming %d bytes.\n", (int)(se
-ibuf
));
119 if (fI
[0] == fI
[1]) {
121 printf("fI[0] == fI[1] == strtof\n");
123 printf("fI[0] == fI[1] = #%lx = %.8g\n",
124 U
*(ULong
*)fI
, fI
[0]);
127 printf("fI[0] = #%lx = %.8g\nfI[1] = #%lx = %.8g\n",
128 U
*(ULong
*)fI
, fI
[0],
129 U
*(ULong
*)&fI
[1], fI
[1]);
131 printf("fI[0] == strtof\n");
132 else if (fI
[1] == u
.f
)
133 printf("fI[1] == strtof\n");
135 printf("**** Both differ from strtof ****\n");