Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "fix printf precision handling" #2906

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,7 @@ static int xisnan(double x) {
0x7ff00000;
}

static size_t mg_dtoa(char *dst, size_t dstlen, double d, int pres, bool tz) {
static size_t mg_dtoa(char *dst, size_t dstlen, double d, int width, bool tz) {
char buf[40];
int i, s = 0, n = 0, e = 0;
double t, mul, saved;
Expand All @@ -1421,21 +1421,21 @@ static size_t mg_dtoa(char *dst, size_t dstlen, double d, int pres, bool tz) {
mul = 1.0;
while (d >= 10.0 && d / mul >= 10.0) mul *= 10.0;
while (d <= 1.0 && d / mul <= 1.0) mul /= 10.0;
for (i = 0, t = mul * 5; i < pres; i++) t /= 10.0;
for (i = 0, t = mul * 5; i < width; i++) t /= 10.0;
d += t;
// Calculate exponent, and 'mul' for scientific representation
mul = 1.0;
while (d >= 10.0 && d / mul >= 10.0) mul *= 10.0, e++;
while (d < 1.0 && d / mul < 1.0) mul /= 10.0, e--;
// printf(" --> %g %d %g %g\n", saved, e, t, mul);

if (tz && e >= pres && pres > 1) {
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, pres, true);
if (e >= width && width > 1) {
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, width, tz);
// printf(" --> %.*g %d [%.*s]\n", 10, d / t, e, n, buf);
n += addexp(buf + s + n, e, '+');
return mg_snprintf(dst, dstlen, "%.*s", n, buf);
} else if (tz && e < 0 && e <= -pres + 1 && pres > 1) {
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, pres, true);
} else if (e <= -width && width > 1) {
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, width, tz);
// printf(" --> %.*g %d [%.*s]\n", 10, d / mul, e, n, buf);
n += addexp(buf + s + n, -e, '-');
return mg_snprintf(dst, dstlen, "%.*s", n, buf);
Expand All @@ -1451,7 +1451,7 @@ static size_t mg_dtoa(char *dst, size_t dstlen, double d, int pres, bool tz) {
while (t >= 1.0 && n + s < (int) sizeof(buf)) buf[n++] = '0', t /= 10.0;
if (s + n < (int) sizeof(buf)) buf[n + s++] = '.';
// printf(" 1--> [%g] -> [%.*s]\n", saved, s + n, buf);
for (i = 0, t = 0.1; s + n < (int) sizeof(buf) && ((tz && (n + 0) < pres) || (!tz && i < pres)); i++) {
for (i = 0, t = 0.1; s + n < (int) sizeof(buf) && n < width; i++) {
int ch = (int) (d / t);
buf[s + n++] = (char) (ch + '0');
d -= ch * t;
Expand Down
14 changes: 7 additions & 7 deletions src/fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static int xisnan(double x) {
0x7ff00000;
}

static size_t mg_dtoa(char *dst, size_t dstlen, double d, int pres, bool tz) {
static size_t mg_dtoa(char *dst, size_t dstlen, double d, int width, bool tz) {
char buf[40];
int i, s = 0, n = 0, e = 0;
double t, mul, saved;
Expand All @@ -51,21 +51,21 @@ static size_t mg_dtoa(char *dst, size_t dstlen, double d, int pres, bool tz) {
mul = 1.0;
while (d >= 10.0 && d / mul >= 10.0) mul *= 10.0;
while (d <= 1.0 && d / mul <= 1.0) mul /= 10.0;
for (i = 0, t = mul * 5; i < pres; i++) t /= 10.0;
for (i = 0, t = mul * 5; i < width; i++) t /= 10.0;
d += t;
// Calculate exponent, and 'mul' for scientific representation
mul = 1.0;
while (d >= 10.0 && d / mul >= 10.0) mul *= 10.0, e++;
while (d < 1.0 && d / mul < 1.0) mul /= 10.0, e--;
// printf(" --> %g %d %g %g\n", saved, e, t, mul);

if (tz && e >= pres && pres > 1) {
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, pres, true);
if (e >= width && width > 1) {
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, width, tz);
// printf(" --> %.*g %d [%.*s]\n", 10, d / t, e, n, buf);
n += addexp(buf + s + n, e, '+');
return mg_snprintf(dst, dstlen, "%.*s", n, buf);
} else if (tz && e < 0 && e <= -pres + 1 && pres > 1) {
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, pres, true);
} else if (e <= -width && width > 1) {
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, width, tz);
// printf(" --> %.*g %d [%.*s]\n", 10, d / mul, e, n, buf);
n += addexp(buf + s + n, -e, '-');
return mg_snprintf(dst, dstlen, "%.*s", n, buf);
Expand All @@ -81,7 +81,7 @@ static size_t mg_dtoa(char *dst, size_t dstlen, double d, int pres, bool tz) {
while (t >= 1.0 && n + s < (int) sizeof(buf)) buf[n++] = '0', t /= 10.0;
if (s + n < (int) sizeof(buf)) buf[n + s++] = '.';
// printf(" 1--> [%g] -> [%.*s]\n", saved, s + n, buf);
for (i = 0, t = 0.1; s + n < (int) sizeof(buf) && ((tz && (n + 0) < pres) || (!tz && i < pres)); i++) {
for (i = 0, t = 0.1; s + n < (int) sizeof(buf) && n < width; i++) {
int ch = (int) (d / t);
buf[s + n++] = (char) (ch + '0');
d -= ch * t;
Expand Down
14 changes: 0 additions & 14 deletions test/unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2127,18 +2127,8 @@ static void test_str(void) {
TESTDOUBLE("%g", 10000.0, "10000");
TESTDOUBLE("%g", 100000.0, "100000");
TESTDOUBLE("%g", 1000000.0, "1e+06");
// TESTDOUBLE("%f", 1000000.0, "1000000");
TESTDOUBLE("%g", 10000000.0, "1e+07");
// TESTDOUBLE("%f", 10000000.0, "10000000");
TESTDOUBLE("%g", 100000001.0, "1e+08");
TESTDOUBLE("%g", 0.1, "0.1");
TESTDOUBLE("%g", 0.01, "0.01");
TESTDOUBLE("%g", 0.001, "0.001");
TESTDOUBLE("%g", 0.0001, "0.0001");
TESTDOUBLE("%g", 0.00001, "1e-05");
TESTDOUBLE("%g", 0.000001, "1e-06");
TESTDOUBLE("%g", -0.0001, "-0.0001");
// TESTDOUBLE("%g", -0.00001, "-1e-05");
TESTDOUBLE("%g", 10.5454, "10.5454");
TESTDOUBLE("%g", 999999.0, "999999");
TESTDOUBLE("%g", 9999999.0, "1e+07");
Expand All @@ -2160,14 +2150,10 @@ static void test_str(void) {
TESTDOUBLE("%.*f", DBLWIDTH(4, 0.14), "0.1400");
TESTDOUBLE("%.*f", DBLWIDTH(3, 0.14), "0.140");
TESTDOUBLE("%.*f", DBLWIDTH(2, 0.14), "0.14");
// TESTDOUBLE("%.*f", DBLWIDTH(2, 25.14), "25.14");
TESTDOUBLE("%.*f", DBLWIDTH(1, 0.14), "0.1");
TESTDOUBLE("%.*f", DBLWIDTH(1, 0.19), "0.2");
TESTDOUBLE("%.*f", DBLWIDTH(1, 0.16), "0.2");
// TESTDOUBLE("%.*f", DBLWIDTH(1, 0.15), "0.1");
// TESTDOUBLE("%.5f", 123.12345, "123.12345");
// TESTDOUBLE("%.4f", 789.01234, "789.0123");
// TESTDOUBLE("%2.3f", 1.23, "1.230");

#ifndef _WIN32
TESTDOUBLE("%g", (double) INFINITY, "inf");
Expand Down
Loading