Skip to content

Commit c4be4e7

Browse files
authored
Merge pull request #2906 from cesanta/revert-2901-fpp
Revert "fix printf precision handling"
2 parents b2bb033 + 877cbbd commit c4be4e7

File tree

3 files changed

+14
-28
lines changed

3 files changed

+14
-28
lines changed

mongoose.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,7 @@ static int xisnan(double x) {
14071407
0x7ff00000;
14081408
}
14091409

1410-
static size_t mg_dtoa(char *dst, size_t dstlen, double d, int pres, bool tz) {
1410+
static size_t mg_dtoa(char *dst, size_t dstlen, double d, int width, bool tz) {
14111411
char buf[40];
14121412
int i, s = 0, n = 0, e = 0;
14131413
double t, mul, saved;
@@ -1421,21 +1421,21 @@ static size_t mg_dtoa(char *dst, size_t dstlen, double d, int pres, bool tz) {
14211421
mul = 1.0;
14221422
while (d >= 10.0 && d / mul >= 10.0) mul *= 10.0;
14231423
while (d <= 1.0 && d / mul <= 1.0) mul /= 10.0;
1424-
for (i = 0, t = mul * 5; i < pres; i++) t /= 10.0;
1424+
for (i = 0, t = mul * 5; i < width; i++) t /= 10.0;
14251425
d += t;
14261426
// Calculate exponent, and 'mul' for scientific representation
14271427
mul = 1.0;
14281428
while (d >= 10.0 && d / mul >= 10.0) mul *= 10.0, e++;
14291429
while (d < 1.0 && d / mul < 1.0) mul /= 10.0, e--;
14301430
// printf(" --> %g %d %g %g\n", saved, e, t, mul);
14311431

1432-
if (tz && e >= pres && pres > 1) {
1433-
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, pres, true);
1432+
if (e >= width && width > 1) {
1433+
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, width, tz);
14341434
// printf(" --> %.*g %d [%.*s]\n", 10, d / t, e, n, buf);
14351435
n += addexp(buf + s + n, e, '+');
14361436
return mg_snprintf(dst, dstlen, "%.*s", n, buf);
1437-
} else if (tz && e < 0 && e <= -pres + 1 && pres > 1) {
1438-
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, pres, true);
1437+
} else if (e <= -width && width > 1) {
1438+
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, width, tz);
14391439
// printf(" --> %.*g %d [%.*s]\n", 10, d / mul, e, n, buf);
14401440
n += addexp(buf + s + n, -e, '-');
14411441
return mg_snprintf(dst, dstlen, "%.*s", n, buf);
@@ -1451,7 +1451,7 @@ static size_t mg_dtoa(char *dst, size_t dstlen, double d, int pres, bool tz) {
14511451
while (t >= 1.0 && n + s < (int) sizeof(buf)) buf[n++] = '0', t /= 10.0;
14521452
if (s + n < (int) sizeof(buf)) buf[n + s++] = '.';
14531453
// printf(" 1--> [%g] -> [%.*s]\n", saved, s + n, buf);
1454-
for (i = 0, t = 0.1; s + n < (int) sizeof(buf) && ((tz && (n + 0) < pres) || (!tz && i < pres)); i++) {
1454+
for (i = 0, t = 0.1; s + n < (int) sizeof(buf) && n < width; i++) {
14551455
int ch = (int) (d / t);
14561456
buf[s + n++] = (char) (ch + '0');
14571457
d -= ch * t;

src/fmt.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static int xisnan(double x) {
3737
0x7ff00000;
3838
}
3939

40-
static size_t mg_dtoa(char *dst, size_t dstlen, double d, int pres, bool tz) {
40+
static size_t mg_dtoa(char *dst, size_t dstlen, double d, int width, bool tz) {
4141
char buf[40];
4242
int i, s = 0, n = 0, e = 0;
4343
double t, mul, saved;
@@ -51,21 +51,21 @@ static size_t mg_dtoa(char *dst, size_t dstlen, double d, int pres, bool tz) {
5151
mul = 1.0;
5252
while (d >= 10.0 && d / mul >= 10.0) mul *= 10.0;
5353
while (d <= 1.0 && d / mul <= 1.0) mul /= 10.0;
54-
for (i = 0, t = mul * 5; i < pres; i++) t /= 10.0;
54+
for (i = 0, t = mul * 5; i < width; i++) t /= 10.0;
5555
d += t;
5656
// Calculate exponent, and 'mul' for scientific representation
5757
mul = 1.0;
5858
while (d >= 10.0 && d / mul >= 10.0) mul *= 10.0, e++;
5959
while (d < 1.0 && d / mul < 1.0) mul /= 10.0, e--;
6060
// printf(" --> %g %d %g %g\n", saved, e, t, mul);
6161

62-
if (tz && e >= pres && pres > 1) {
63-
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, pres, true);
62+
if (e >= width && width > 1) {
63+
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, width, tz);
6464
// printf(" --> %.*g %d [%.*s]\n", 10, d / t, e, n, buf);
6565
n += addexp(buf + s + n, e, '+');
6666
return mg_snprintf(dst, dstlen, "%.*s", n, buf);
67-
} else if (tz && e < 0 && e <= -pres + 1 && pres > 1) {
68-
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, pres, true);
67+
} else if (e <= -width && width > 1) {
68+
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, width, tz);
6969
// printf(" --> %.*g %d [%.*s]\n", 10, d / mul, e, n, buf);
7070
n += addexp(buf + s + n, -e, '-');
7171
return mg_snprintf(dst, dstlen, "%.*s", n, buf);
@@ -81,7 +81,7 @@ static size_t mg_dtoa(char *dst, size_t dstlen, double d, int pres, bool tz) {
8181
while (t >= 1.0 && n + s < (int) sizeof(buf)) buf[n++] = '0', t /= 10.0;
8282
if (s + n < (int) sizeof(buf)) buf[n + s++] = '.';
8383
// printf(" 1--> [%g] -> [%.*s]\n", saved, s + n, buf);
84-
for (i = 0, t = 0.1; s + n < (int) sizeof(buf) && ((tz && (n + 0) < pres) || (!tz && i < pres)); i++) {
84+
for (i = 0, t = 0.1; s + n < (int) sizeof(buf) && n < width; i++) {
8585
int ch = (int) (d / t);
8686
buf[s + n++] = (char) (ch + '0');
8787
d -= ch * t;

test/unit_test.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,18 +2127,8 @@ static void test_str(void) {
21272127
TESTDOUBLE("%g", 10000.0, "10000");
21282128
TESTDOUBLE("%g", 100000.0, "100000");
21292129
TESTDOUBLE("%g", 1000000.0, "1e+06");
2130-
// TESTDOUBLE("%f", 1000000.0, "1000000");
21312130
TESTDOUBLE("%g", 10000000.0, "1e+07");
2132-
// TESTDOUBLE("%f", 10000000.0, "10000000");
21332131
TESTDOUBLE("%g", 100000001.0, "1e+08");
2134-
TESTDOUBLE("%g", 0.1, "0.1");
2135-
TESTDOUBLE("%g", 0.01, "0.01");
2136-
TESTDOUBLE("%g", 0.001, "0.001");
2137-
TESTDOUBLE("%g", 0.0001, "0.0001");
2138-
TESTDOUBLE("%g", 0.00001, "1e-05");
2139-
TESTDOUBLE("%g", 0.000001, "1e-06");
2140-
TESTDOUBLE("%g", -0.0001, "-0.0001");
2141-
// TESTDOUBLE("%g", -0.00001, "-1e-05");
21422132
TESTDOUBLE("%g", 10.5454, "10.5454");
21432133
TESTDOUBLE("%g", 999999.0, "999999");
21442134
TESTDOUBLE("%g", 9999999.0, "1e+07");
@@ -2160,14 +2150,10 @@ static void test_str(void) {
21602150
TESTDOUBLE("%.*f", DBLWIDTH(4, 0.14), "0.1400");
21612151
TESTDOUBLE("%.*f", DBLWIDTH(3, 0.14), "0.140");
21622152
TESTDOUBLE("%.*f", DBLWIDTH(2, 0.14), "0.14");
2163-
// TESTDOUBLE("%.*f", DBLWIDTH(2, 25.14), "25.14");
21642153
TESTDOUBLE("%.*f", DBLWIDTH(1, 0.14), "0.1");
21652154
TESTDOUBLE("%.*f", DBLWIDTH(1, 0.19), "0.2");
21662155
TESTDOUBLE("%.*f", DBLWIDTH(1, 0.16), "0.2");
21672156
// TESTDOUBLE("%.*f", DBLWIDTH(1, 0.15), "0.1");
2168-
// TESTDOUBLE("%.5f", 123.12345, "123.12345");
2169-
// TESTDOUBLE("%.4f", 789.01234, "789.0123");
2170-
// TESTDOUBLE("%2.3f", 1.23, "1.230");
21712157

21722158
#ifndef _WIN32
21732159
TESTDOUBLE("%g", (double) INFINITY, "inf");

0 commit comments

Comments
 (0)