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_ffmt, strtof, strtoIf, strtopf, and strtorf.
|
---|
39 | *
|
---|
40 | * Inputs (on stdin):
|
---|
41 | * r rounding_mode
|
---|
42 | * n ndig
|
---|
43 | * number
|
---|
44 | * #hex
|
---|
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 | * hex is a string of <= 8 Hex digits for the internal representation
|
---|
54 | * of the number, and ndig is a parameters to g_ffmt.
|
---|
55 | */
|
---|
56 |
|
---|
57 | #include "gdtoa.h"
|
---|
58 | #include <stdio.h>
|
---|
59 | #include <stdlib.h>
|
---|
60 |
|
---|
61 | extern int getround ANSI((int,char*));
|
---|
62 |
|
---|
63 | static char ibuf[2048], obuf[1024];
|
---|
64 |
|
---|
65 | #define U (unsigned long)
|
---|
66 |
|
---|
67 | int
|
---|
68 | main(Void)
|
---|
69 | {
|
---|
70 | ULong *L;
|
---|
71 | char *s, *se, *se1;
|
---|
72 | int dItry, i, i1, ndig = 0, r = 1;
|
---|
73 | float f, f1, fI[2];
|
---|
74 |
|
---|
75 | L = (ULong*)&f;
|
---|
76 | while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) {
|
---|
77 | while(*s <= ' ')
|
---|
78 | if (!*s++)
|
---|
79 | continue;
|
---|
80 | dItry = 0;
|
---|
81 | switch(*s) {
|
---|
82 | case 'r':
|
---|
83 | r = getround(r, s);
|
---|
84 | continue;
|
---|
85 | case 'n':
|
---|
86 | i = s[1];
|
---|
87 | if (i <= ' ' || i >= '0' && i <= '9') {
|
---|
88 | ndig = atoi(s+1);
|
---|
89 | continue;
|
---|
90 | }
|
---|
91 | break; /* nan? */
|
---|
92 | case '#':
|
---|
93 | sscanf(s+1, "%lx", &L[0]);
|
---|
94 | printf("\nInput: %s", ibuf);
|
---|
95 | printf(" --> f = #%lx\n", L[0]);
|
---|
96 | goto fmt_test;
|
---|
97 | }
|
---|
98 | dItry = 1;
|
---|
99 | printf("\nInput: %s", ibuf);
|
---|
100 | i = strtorf(ibuf, &se, r, &f);
|
---|
101 | if (r == 1) {
|
---|
102 | if (f != (i1 = strtopf(ibuf, &se1, &f1), f1)
|
---|
103 | || se != se1 || i != i1) {
|
---|
104 | printf("***strtopf and strtorf disagree!!\n");
|
---|
105 | if (f != f1)
|
---|
106 | printf("\tf1 = %g\n", (double)f1);
|
---|
107 | if (i != i1)
|
---|
108 | printf("\ti = %d but i1 = %d\n", i, i1);
|
---|
109 | if (se != se1)
|
---|
110 | printf("se - se1 = %d\n", (int)(se-se1));
|
---|
111 | }
|
---|
112 | if (f != strtof(ibuf, &se1) || se != se1)
|
---|
113 | printf("***strtof and strtorf disagree!\n");
|
---|
114 | }
|
---|
115 | printf("strtof consumes %d bytes and returns %.8g = #%lx\n",
|
---|
116 | (int)(se-ibuf), f, U *(ULong*)&f);
|
---|
117 | fmt_test:
|
---|
118 | se = g_ffmt(obuf, &f, ndig, sizeof(obuf));
|
---|
119 | printf("g_ffmt(%d) gives %d bytes: \"%s\"\n\n",
|
---|
120 | ndig, (int)(se-obuf), se ? obuf : "<null>");
|
---|
121 | if (!dItry)
|
---|
122 | continue;
|
---|
123 | printf("strtoIf returns %d,", strtoIf(ibuf, &se, fI, &fI[1]));
|
---|
124 | printf(" consuming %d bytes.\n", (int)(se-ibuf));
|
---|
125 | if (fI[0] == fI[1]) {
|
---|
126 | if (fI[0] == f)
|
---|
127 | printf("fI[0] == fI[1] == strtof\n");
|
---|
128 | else
|
---|
129 | printf("fI[0] == fI[1] = #%lx = %.8g\n",
|
---|
130 | U *(ULong*)fI, fI[0]);
|
---|
131 | }
|
---|
132 | else {
|
---|
133 | printf("fI[0] = #%lx = %.8g\nfI[1] = #%lx = %.8g\n",
|
---|
134 | U *(ULong*)fI, fI[0],
|
---|
135 | U *(ULong*)&fI[1], fI[1]);
|
---|
136 | if (fI[0] == f)
|
---|
137 | printf("fI[0] == strtof\n");
|
---|
138 | else if (fI[1] == f)
|
---|
139 | printf("fI[1] == strtof\n");
|
---|
140 | else
|
---|
141 | printf("**** Both differ from strtof ****\n");
|
---|
142 | }
|
---|
143 | printf("\n");
|
---|
144 | }
|
---|
145 | return 0;
|
---|
146 | }
|
---|