| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
#include <stdio.h> |
| 46 |
#include "gdtoa.h" |
| 47 |
int STRTOD_DIGLIM = 24; |
| 48 |
#ifdef KR_headers |
| 49 |
#define Void |
| 50 |
#else |
| 51 |
#define Void void |
| 52 |
#endif |
| 53 |
|
| 54 |
#ifdef __STDC__ |
| 55 |
#include <stdlib.h> |
| 56 |
#else |
| 57 |
#ifdef __cplusplus |
| 58 |
extern "C" double atof(const char*); |
| 59 |
#else |
| 60 |
extern double atof ANSI((char*)); |
| 61 |
#endif |
| 62 |
#endif |
| 63 |
|
| 64 |
typedef union { double d; ULong L[2]; } U; |
| 65 |
|
| 66 |
#ifdef IEEE_8087 |
| 67 |
#define word0(x) (x)->L[1] |
| 68 |
#define word1(x) (x)->L[0] |
| 69 |
#else |
| 70 |
#define word0(x) (x)->L[0] |
| 71 |
#define word1(x) (x)->L[1] |
| 72 |
#endif |
| 73 |
#define dval(x) (x)->d |
| 74 |
|
| 75 |
#include "errno.h" |
| 76 |
|
| 77 |
#ifdef __cplusplus |
| 78 |
extern "C" char *dtoa(double, int, int, int*, int*, char **); |
| 79 |
#else |
| 80 |
extern char *dtoa ANSI((double, int, int, int*, int*, char **)); |
| 81 |
#endif |
| 82 |
|
| 83 |
static void |
| 84 |
#ifdef KR_headers |
| 85 |
g_fmt(b, x) char *b; double x; |
| 86 |
#else |
| 87 |
g_fmt(char *b, double x) |
| 88 |
#endif |
| 89 |
{ |
| 90 |
char *s, *se; |
| 91 |
int decpt, i, j, k, sign; |
| 92 |
|
| 93 |
if (!x) { |
| 94 |
*b++ = '0'; |
| 95 |
*b = 0; |
| 96 |
return; |
| 97 |
} |
| 98 |
s = dtoa(x, 0, 0, &decpt, &sign, &se); |
| 99 |
if (sign) |
| 100 |
*b++ = '-'; |
| 101 |
if (decpt == 9999) { |
| 102 |
while((*b++ = *s++)); |
| 103 |
return; |
| 104 |
} |
| 105 |
if (decpt <= -4 || decpt > se - s + 5) { |
| 106 |
*b++ = *s++; |
| 107 |
if (*s) { |
| 108 |
*b++ = '.'; |
| 109 |
while((*b = *s++)) |
| 110 |
b++; |
| 111 |
} |
| 112 |
*b++ = 'e'; |
| 113 |
|
| 114 |
if (--decpt < 0) { |
| 115 |
*b++ = '-'; |
| 116 |
decpt = -decpt; |
| 117 |
} |
| 118 |
else |
| 119 |
*b++ = '+'; |
| 120 |
for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10){}; |
| 121 |
for(;;) { |
| 122 |
i = decpt / k; |
| 123 |
*b++ = i + '0'; |
| 124 |
if (--j <= 0) |
| 125 |
break; |
| 126 |
decpt -= i*k; |
| 127 |
decpt *= 10; |
| 128 |
} |
| 129 |
*b = 0; |
| 130 |
} |
| 131 |
else if (decpt <= 0) { |
| 132 |
*b++ = '.'; |
| 133 |
for(; decpt < 0; decpt++) |
| 134 |
*b++ = '0'; |
| 135 |
while((*b++ = *s++)); |
| 136 |
} |
| 137 |
else { |
| 138 |
while((*b = *s++)) { |
| 139 |
b++; |
| 140 |
if (--decpt == 0 && *s) |
| 141 |
*b++ = '.'; |
| 142 |
} |
| 143 |
for(; decpt > 0; decpt--) |
| 144 |
*b++ = '0'; |
| 145 |
*b = 0; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
static void |
| 150 |
baderrno(Void) |
| 151 |
{ |
| 152 |
fflush(stdout); |
| 153 |
perror("\nerrno strtod"); |
| 154 |
fflush(stderr); |
| 155 |
} |
| 156 |
|
| 157 |
#define UL (unsigned long) |
| 158 |
|
| 159 |
static void |
| 160 |
#ifdef KR_headers |
| 161 |
check(d) U *d; |
| 162 |
#else |
| 163 |
check(U *d) |
| 164 |
#endif |
| 165 |
{ |
| 166 |
char buf[64]; |
| 167 |
int decpt, sign; |
| 168 |
char *s, *se; |
| 169 |
U d1; |
| 170 |
|
| 171 |
s = dtoa(dval(d), 0, 0, &decpt, &sign, &se); |
| 172 |
sprintf(buf, "%s%s%se%d", sign ? "-" : "", |
| 173 |
decpt == 9999 ? "" : ".", s, decpt); |
| 174 |
errno = 0; |
| 175 |
dval(&d1) = strtod(buf, (char **)0); |
| 176 |
if (errno) |
| 177 |
baderrno(); |
| 178 |
if (dval(d) != dval(&d1)) { |
| 179 |
printf("sent d = %.17g = 0x%lx %lx, buf = %s\n", |
| 180 |
dval(d), UL word0(d), UL word1(d), buf); |
| 181 |
printf("got d1 = %.17g = 0x%lx %lx\n", |
| 182 |
dval(&d1), UL word0(&d1), UL word1(&d1)); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
#ifdef USE_fpinit |
| 187 |
extern void fpinit_ASL(); |
| 188 |
|
| 189 |
|
| 190 |
#else |
| 191 |
#define fpinit_ASL() |
| 192 |
#endif |
| 193 |
|
| 194 |
int |
| 195 |
main(Void) |
| 196 |
{ |
| 197 |
U d, d1; |
| 198 |
char buf[2048], buf1[32]; |
| 199 |
char *fmt, *s, *s1, *se; |
| 200 |
int decpt, sign; |
| 201 |
int mode = 0, ndigits = 17; |
| 202 |
ULong x, y; |
| 203 |
#ifdef VAX |
| 204 |
ULong z; |
| 205 |
#endif |
| 206 |
|
| 207 |
fpinit_ASL(); |
| 208 |
d.d = 0.; |
| 209 |
while(fgets(buf, sizeof(buf), stdin)) { |
| 210 |
if (*buf == '*') { |
| 211 |
printf("%s", buf); |
| 212 |
continue; |
| 213 |
} |
| 214 |
printf("Input: %s", buf); |
| 215 |
if (*buf == '#') { |
| 216 |
x = word0(&d); |
| 217 |
y = word1(&d); |
| 218 |
|
| 219 |
x = (ULong)strtoul(s1 = buf+1, &se, 16); |
| 220 |
if (se > s1) { |
| 221 |
y = (ULong)strtoul(s1 = se, &se, 16); |
| 222 |
if (se > s1) |
| 223 |
sscanf(se, ":%d %d", &mode, &ndigits); |
| 224 |
} |
| 225 |
word0(&d) = x; |
| 226 |
word1(&d) = y; |
| 227 |
fmt = "Output: d =\n%.17g = 0x%lx %lx\n"; |
| 228 |
} |
| 229 |
else if (*buf == '*') { |
| 230 |
x = strtoul(buf,&s,10); |
| 231 |
if (!*s && x > 18) |
| 232 |
STRTOD_DIGLIM = (int)x; |
| 233 |
printf("STRTOD_DIGLIM = %lu\n", UL x); |
| 234 |
continue; |
| 235 |
} |
| 236 |
else { |
| 237 |
errno = 0; |
| 238 |
dval(&d) = strtod(buf,&se); |
| 239 |
if (*se == ':') |
| 240 |
sscanf(se+1,"%d %d", &mode, &ndigits); |
| 241 |
dval(&d1) = atof(buf); |
| 242 |
fmt = "Output: d =\n%.17g = 0x%lx %lx, se = %s"; |
| 243 |
if (errno) |
| 244 |
baderrno(); |
| 245 |
} |
| 246 |
printf(fmt, dval(&d), UL word0(&d), UL word1(&d), se); |
| 247 |
g_fmt(buf1, dval(&d)); |
| 248 |
printf("\tg_fmt gives \"%s\"\n", buf1); |
| 249 |
if (*buf != '#' && dval(&d) != dval(&d1)) |
| 250 |
printf("atof gives\n\ |
| 251 |
d1 = %.17g = 0x%lx %lx\nversus\n\ |
| 252 |
d = %.17g = 0x%lx %lx\n", dval(&d1), UL word0(&d1), UL word1(&d1), |
| 253 |
dval(&d), UL word0(&d), UL word1(&d)); |
| 254 |
check(&d); |
| 255 |
s = dtoa(dval(&d), mode, ndigits, &decpt, &sign, &se); |
| 256 |
printf("\tdtoa(mode = %d, ndigits = %d):\n", mode, ndigits); |
| 257 |
printf("\tdtoa returns sign = %d, decpt = %d, %d digits:\n%s\n", |
| 258 |
sign, decpt, (int)(se-s), s); |
| 259 |
x = word1(&d); |
| 260 |
if (x != 0xffffffff |
| 261 |
&& (word0(&d) & 0x7ff00000) != 0x7ff00000) { |
| 262 |
#ifdef VAX |
| 263 |
z = x << 16 | x >> 16; |
| 264 |
z++; |
| 265 |
z = z << 16 | z >> 16; |
| 266 |
word1(&d) = z; |
| 267 |
#else |
| 268 |
word1(&d) = x + 1; |
| 269 |
#endif |
| 270 |
printf("\tnextafter(d,+Inf) = %.17g = 0x%lx %lx:\n", |
| 271 |
dval(&d), UL word0(&d), UL word1(&d)); |
| 272 |
g_fmt(buf1, dval(&d)); |
| 273 |
printf("\tg_fmt gives \"%s\"\n", buf1); |
| 274 |
s = dtoa(dval(&d), mode, ndigits, &decpt, &sign, &se); |
| 275 |
printf( |
| 276 |
"\tdtoa returns sign = %d, decpt = %d, %d digits:\n%s\n", |
| 277 |
sign, decpt, (int)(se-s), s); |
| 278 |
check(&d); |
| 279 |
} |
| 280 |
if (x) { |
| 281 |
#ifdef VAX |
| 282 |
z = x << 16 | x >> 16; |
| 283 |
z--; |
| 284 |
z = z << 16 | z >> 16; |
| 285 |
word1(&d) = z; |
| 286 |
#else |
| 287 |
word1(&d) = x - 1; |
| 288 |
#endif |
| 289 |
printf("\tnextafter(d,-Inf) = %.17g = 0x%lx %lx:\n", |
| 290 |
dval(&d), UL word0(&d), UL word1(&d)); |
| 291 |
g_fmt(buf1, dval(&d)); |
| 292 |
printf("\tg_fmt gives \"%s\"\n", buf1); |
| 293 |
s = dtoa(dval(&d), mode, ndigits, &decpt, &sign, &se); |
| 294 |
printf( |
| 295 |
"\tdtoa returns sign = %d, decpt = %d, %d digits:\n%s\n", |
| 296 |
sign, decpt, (int)(se-s), s); |
| 297 |
check(&d); |
| 298 |
} |
| 299 |
} |
| 300 |
return 0; |
| 301 |
} |