tformat string options: remove newline and use consistent output format - numto… | |
git clone git://src.adamsgaard.dk/numtools | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit d97e551d2b850c6ce5f835281beec71baa94f4c0 | |
parent ad31fb4193a02125008316ccd2d074f1bbd89f56 | |
Author: Anders Damsgaard <[email protected]> | |
Date: Mon, 9 May 2022 17:52:08 +0200 | |
format string options: remove newline and use consistent output format | |
Diffstat: | |
M randnum.1 | 4 ++-- | |
M randnum.c | 6 ++++-- | |
M range.1 | 17 ++--------------- | |
M range.c | 2 +- | |
M rangetest.c | 7 ++++--- | |
M util.c | 4 ++-- | |
6 files changed, 15 insertions(+), 25 deletions(-) | |
--- | |
diff --git a/randnum.1 b/randnum.1 | |
t@@ -47,7 +47,7 @@ When including a format specifier (%..), only use forms that… | |
compatible with | |
.Vt double | |
types. | |
-The default format string is '%.17g\n'. | |
+The default format string is '%.17g'. | |
.It Fl h | |
Show usage information. | |
.It Fl n Ar num | |
t@@ -64,7 +64,7 @@ Due to the randomness, your output may differ: | |
.Dl 0.38385568287140259 | |
.Pp | |
Generate five points in the range [-10;10[ and print with three significant di… | |
-.Dl $ randnum -n 5 -f '%.3g\n' -- -10 10 | |
+.Dl $ randnum -n 5 -f '%.3g' -- -10 10 | |
.Dl -4.16 | |
.Dl -3.36 | |
.Dl -5.8 | |
diff --git a/randnum.c b/randnum.c | |
t@@ -24,7 +24,7 @@ main(int argc, char *argv[]) | |
int i, ret, s = 0; | |
long j, seed, n = 1; | |
double val, minv = 0.0, maxv = 1.0; | |
- char fmtstr[PATH_MAX] = "%.17g\n"; | |
+ char fmtstr[PATH_MAX] = "%.17g"; | |
struct timeval t1; | |
if (pledge("stdio", NULL) == -1) | |
t@@ -69,8 +69,10 @@ main(int argc, char *argv[]) | |
} | |
#endif | |
- for (i = 0; i < n; i++) | |
+ for (i = 0; i < n; i++) { | |
printf(fmtstr, drand48() * (maxv - minv) + minv); | |
+ putchar('\n'); | |
+ } | |
return 0; | |
} | |
diff --git a/range.1 b/range.1 | |
t@@ -60,7 +60,7 @@ When including a format specifier (%..), only use forms that… | |
compatible with | |
.Vt double | |
types. | |
-The default format string is '%g\en'. | |
+The default format string is '%.17g'. | |
.It Fl h | |
Show usage information. | |
.It Fl l | |
t@@ -74,15 +74,6 @@ Print the spacing between numbers and exit. | |
.El | |
.Sh EXAMPLES | |
Generate four equally-spaced numbers in the closed default range [0;1]: | |
-.Dl $ range -n 4 | |
-.Dl 0 | |
-.Dl 0.33333 | |
-.Dl 0.66667 | |
-.Dl 1 | |
-.Pp | |
-Same as the previous example, but with full | |
-.Vt double | |
-precision on a 64-bit system: | |
.Dl $ range -n 4 -f '%.17g\en' 0 1 | |
.Dl 0 | |
.Dl 0.33333333333333331 | |
t@@ -96,12 +87,8 @@ Generate four numbers in the range ]0;1[: | |
.Dl 0.6 | |
.Dl 0.8 | |
.Pp | |
-Print ten numbers in the interval [1;10] with spaces between values: | |
-.Dl $ range -f '%g ' 1 10 | |
-.Dl 1 2 3 4 5 6 7 8 9 10 | |
-.Pp | |
Repeat and modify a string three times: | |
-.Dl $ range -n 3 -f 'The best number is %.0g\en' 1 3 | |
+.Dl $ range -n 3 -f 'The best number is %.0g' 1 3 | |
.Dl The best number is 1 | |
.Dl The best number is 2 | |
.Dl The best number is 3 | |
diff --git a/range.c b/range.c | |
t@@ -23,7 +23,7 @@ main(int argc, char *argv[]) | |
int i, ret, n = 10, logrange = 0, openstart = 0, openend = 0, | |
reportdx = 0; | |
double minv = 0.0, maxv = 1.0, dx; | |
- char fmtstr[PATH_MAX] = "%g\n"; | |
+ char fmtstr[PATH_MAX] = "%.17g"; | |
if (pledge("stdio", NULL) == -1) | |
err(2, "pledge"); | |
diff --git a/rangetest.c b/rangetest.c | |
t@@ -27,12 +27,13 @@ launch(char *cmd, char *cmd0, double val) | |
errx(1, VALUESTR " not found in cmd"); | |
if (strlcpy(cmd, cmd0, PATH_MAX) >= PATH_MAX) | |
err(1, "cmd too long"); | |
- sprintf(cmd + (c - cmd0), "%g%s >/dev/null", val, c + strnlen(VALUESTR… | |
+ sprintf(cmd + (c - cmd0), "%.17g%s >/dev/null", | |
+ val, c + strnlen(VALUESTR, sizeof(cmd))); | |
if (system(cmd)) { | |
- fprintf(stderr, "%g\n", val); | |
+ fprintf(stderr, "%.17g\n", val); | |
return 1; | |
} else | |
- printf("%g\n", val); | |
+ printf("%.17g\n", val); | |
return 0; | |
} | |
diff --git a/util.c b/util.c | |
t@@ -41,7 +41,7 @@ printarr(double *arr, size_t len) | |
if (i < len) | |
printf(DELIMSTR); | |
} | |
- puts(""); | |
+ putchar('\n'); | |
} | |
void | |
t@@ -54,7 +54,7 @@ printfarr(char *fmtstr, double *arr, size_t len) | |
if (i < len) | |
printf(DELIMSTR); | |
} | |
- puts(""); | |
+ putchar('\n'); | |
} | |
size_t |