#include "gdtoaimp.h"
#include <stdio.h>
#include <stdlib.h>
extern int getround ANSI((int,char*));
static char ibuf[2048], obuf[1024];
#define U (unsigned long)
int
main(Void)
{
char *s, *se, *se1;
double f1, fI[2];
int i, i1, ndig = 0, r = 1;
long LL[2];
union { double f; ULong L[2]; } u;
while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) {
while(*s <= ' ')
if (!*s++)
continue;
switch(*s) {
case 'r':
r = getround(r, s);
continue;
case 'n':
i = s[1];
if (i <= ' ' || (i >= '0' && i <= '9')) {
ndig = atoi(s+1);
continue;
}
break;
case '#':
LL[0] = u.L[_0];
LL[1] = u.L[_1];
sscanf(s+1, "%lx %lx", &LL[0], &LL[1]);
u.L[_0] = LL[0];
u.L[_1] = LL[1];
printf("\nInput: %s", ibuf);
printf("--> f = #%lx %lx\n", (long)u.L[_0], (long)u.L[_1]);
goto fmt_test;
}
printf("\nInput: %s", ibuf);
i = strtord(ibuf, &se, r, &u.f);
if (r == 1) {
if ((u.f != strtod(ibuf, &se1) || se1 != se))
printf("***strtod and strtord disagree!!\n");
i1 = strtopd(ibuf, &se, &f1);
if (i != i1 || u.f != f1 || se != se1)
printf("***strtord and strtopd disagree!!\n");
}
printf("strtod consumes %d bytes and returns %d with f = %.17g = #%lx %lx\n",
(int)(se-ibuf), i, u.f, U u.L[_0], U u.L[_1]);
fmt_test:
se = g_dfmt(obuf, &u.f, ndig, sizeof(obuf));
printf("g_dfmt(%d) gives %d bytes: \"%s\"\n\n",
ndig, (int)(se-obuf), se ? obuf : "<null>");
if (*s == '#')
continue;
printf("strtoId returns %d,", strtoId(ibuf, &se, fI, &fI[1]));
printf(" consuming %d bytes.\n", (int)(se-ibuf));
if (fI[0] == fI[1]) {
if (fI[0] == u.f)
printf("fI[0] == fI[1] == strtod\n");
else
printf("fI[0] == fI[1] = #%lx %lx = %.17g\n",
U ((ULong*)fI)[_0], U ((ULong*)fI)[_1],
fI[0]);
}
else {
printf("fI[0] = #%lx %lx = %.17g\n",
U ((ULong*)fI)[_0], U ((ULong*)fI)[_1], fI[0]);
printf("fI[1] = #%lx %lx = %.17g\n",
U ((ULong*)&fI[1])[_0], U ((ULong*)&fI[1])[_1],
fI[1]);
if (fI[0] == u.f)
printf("fI[0] == strtod\n");
else if (fI[1] == u.f)
printf("fI[1] == strtod\n");
else
printf("**** Both differ from strtod ****\n");
}
printf("\n");
}
return 0;
}