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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
#include <u.h>
#include <libc.h>
#include <stdio.h>
/*
* try all combination of flags and float conversions
* with some different widths & precisions
*/
#define Njust 2
#define Nplus 3
#define Nalt 2
#define Nzero 2
#define Nspec 5
#define Nwidth 5
#define Nprec 5
static double fmtvals[] = {
3.1415925535897932e15,
3.1415925535897932e14,
3.1415925535897932e13,
3.1415925535897932e12,
3.1415925535897932e11,
3.1415925535897932e10,
3.1415925535897932e9,
3.1415925535897932e8,
3.1415925535897932e7,
3.1415925535897932e6,
3.1415925535897932e5,
3.1415925535897932e4,
3.1415925535897932e3,
3.1415925535897932e2,
3.1415925535897932e1,
3.1415925535897932e0,
3.1415925535897932e-1,
3.1415925535897932e-2,
3.1415925535897932e-3,
3.1415925535897932e-4,
3.1415925535897932e-5,
3.1415925535897932e-6,
3.1415925535897932e-7,
3.1415925535897932e-8,
3.1415925535897932e-9,
3.1415925535897932e-10,
3.1415925535897932e-11,
3.1415925535897932e-12,
3.1415925535897932e-13,
3.1415925535897932e-14,
3.1415925535897932e-15,
};
/*
* are the numbers close?
* used to compare long numbers where the last few digits are garbage
* due to precision problems
*/
static int
numclose(char *num1, char *num2)
{
int ndig;
double d1, d2;
enum { MAXDIG = 15 };
d1 = fmtstrtod(num1, 0);
d2 = fmtstrtod(num2, 0);
if(d1 != d2)
return 0;
ndig = 0;
while (*num1) {
if (*num1 >= '0' && *num1 <= '9') {
ndig++;
if (ndig > MAXDIG) {
if (!(*num2 >= '0' && *num2 <= '9')) {
return 0;
}
} else if (*num1 != *num2) {
return 0;
}
} else if (*num1 != *num2) {
return 0;
} else if (*num1 == 'e' || *num1 == 'E') {
ndig = 0;
}
num1++;
num2++;
}
if (*num1 || !num2)
return 0;
return 1;
}
static void
doit(int just, int plus, int alt, int zero, int width, int prec, int spec)
{
char format[256];
char *p;
const char *s;
int i;
p = format;
*p++ = '%';
if (just > 0)
*p++ = "-"[just - 1];
if (plus > 0)
*p++ = "+ "[plus - 1];
if (alt > 0)
*p++ = "#"[alt - 1];
if (zero > 0)
*p++ = "0"[zero - 1];
s = "";
switch (width) {
case 1: s = "1"; break;
case 2: s = "5"; break;
case 3: s = "10"; break;
case 4: s = "15"; break;
}
strcpy(p, s);
s = "";
switch (prec) {
case 1: s = ".0"; break;
case 2: s = ".2"; break;
case 3: s = ".5"; break;
case 4: s = ".15"; break;
}
strcat(p, s);
p = strchr(p, '\0');
*p++ = "efgEG"[spec];
*p = '\0';
for (i = 0; i < sizeof(fmtvals) / sizeof(fmtvals[0]); i++) {
char ref[1024], buf[1024];
Rune rbuf[1024];
double d1, d2;
sprintf(ref, format, fmtvals[i]);
snprint(buf, sizeof(buf), format, fmtvals[i]);
if (strcmp(ref, buf) != 0
&& !numclose(ref, buf)) {
d1 = fmtstrtod(ref, 0);
d2 = fmtstrtod(buf, 0);
fprintf(stderr, "%s: ref='%s'%s fmt='%s'%s\n",
format,
ref, d1==fmtvals[i] ? "" : " (ref is inexact!)",
buf, d2==fmtvals[i] ? "" : " (fmt is inexact!)");
// exits("oops");
}
/* Check again with output to rune string */
runesnprint(rbuf, 1024, format, fmtvals[i]);
snprint(buf, sizeof(buf), "%S", rbuf);
if (strcmp(ref, buf) != 0
&& !numclose(ref, buf)) {
d1 = fmtstrtod(ref, 0);
d2 = fmtstrtod(buf, 0);
fprintf(stderr, "%s: ref='%s'%s fmt='%s'%s\n",
format,
ref, d1==fmtvals[i] ? "" : " (ref is inexact!)",
buf, d2==fmtvals[i] ? "" : " (fmt is inexact!)");
// exits("oops");
}
}
}
void
main(int argc, char **argv)
{
int just, plus, alt, zero, width, prec, spec;
for (just = 0; just < Njust; just++)
for (plus = 0; plus < Nplus; plus++)
for (alt = 0; alt < Nalt; alt++)
for (zero = 0; zero < Nzero; zero++)
for (width = 0; width < Nwidth; width++)
for (prec = 0; prec < Nprec; prec++)
for (spec = 0; spec < Nspec; spec++)
doit(just, plus, alt, zero, width, prec, spec);
exits(0);
}
|