Fix printing
diff --git a/include/num.h b/include/num.h
index 805d1ba..c1837e3 100644
--- a/include/num.h
+++ b/include/num.h
@@ -61,7 +61,7 @@
typedef BcStatus (*BcNumUnaryFunc)(BcNum*, BcNum*, size_t);
typedef BcStatus (*BcNumBinaryFunc)(BcNum*, BcNum*, BcNum*, size_t);
-typedef BcStatus (*BcNumDigitFunc)(unsigned long, size_t, size_t*, FILE*);
+typedef BcStatus (*BcNumDigitFunc)(unsigned long, size_t, bool, size_t*, FILE*);
BcStatus bc_num_init(BcNum *n, size_t request);
diff --git a/src/bc/num.c b/src/bc/num.c
index afe585d..5995565 100644
--- a/src/bc/num.c
+++ b/src/bc/num.c
@@ -1249,22 +1249,7 @@
return status;
}
-static BcStatus bc_num_printRadix(size_t *nchars, FILE *f) {
-
- if (*nchars + 1 >= BC_NUM_PRINT_WIDTH) {
- if (fputc('\\', f) == EOF) return BC_STATUS_IO_ERR;
- if (fputc('\n', f) == EOF) return BC_STATUS_IO_ERR;
- *nchars = 0;
- }
-
- if (fputc('.', f) == EOF) return BC_STATUS_IO_ERR;
-
- *nchars = *nchars + 1;
-
- return BC_STATUS_SUCCESS;
-}
-
-static BcStatus bc_num_printDigits(unsigned long num, size_t width,
+static BcStatus bc_num_printDigits(unsigned long num, size_t width, bool radix,
size_t *nchars, FILE *f)
{
if (*nchars + width + 1 >= BC_NUM_PRINT_WIDTH) {
@@ -1273,7 +1258,7 @@
*nchars = 0;
}
else {
- if (fputc(' ', f) == EOF) return BC_STATUS_IO_ERR;
+ if (fputc(radix ? '.' : ' ', f) == EOF) return BC_STATUS_IO_ERR;
++(*nchars);
}
@@ -1285,15 +1270,17 @@
return BC_STATUS_SUCCESS;
}
-static BcStatus bc_num_printHex(unsigned long num, size_t width,
+static BcStatus bc_num_printHex(unsigned long num, size_t width, bool radix,
size_t *nchars, FILE *f)
{
- if (*nchars + width >= BC_NUM_PRINT_WIDTH) {
+ if (*nchars + width + !!radix >= BC_NUM_PRINT_WIDTH) {
if (fputc('\\', f) == EOF) return BC_STATUS_IO_ERR;
if (fputc('\n', f) == EOF) return BC_STATUS_IO_ERR;
*nchars = 0;
}
+ if (radix && fputc('.', f) == EOF) return BC_STATUS_IO_ERR;
+
if (fputc(bc_num_hex_digits[num], f) == EOF) return BC_STATUS_IO_ERR;
*nchars = *nchars + width;
@@ -1306,6 +1293,7 @@
BcStatus status;
size_t i;
size_t nchars;
+ bool radix;
nchars = 0;
@@ -1317,16 +1305,12 @@
status = BC_STATUS_SUCCESS;
for (i = n->len - 1; !status && i >= n->rdx && i < n->len; --i)
- status = bc_num_printHex(n->num[i], 1, &nchars, f);
+ status = bc_num_printHex(n->num[i], 1, false, &nchars, f);
if (status || !n->rdx) return status;
- status = bc_num_printRadix(&nchars, f);
-
- if (status) return status;
-
- for (; !status && i < n->len; --i)
- status = bc_num_printHex(n->num[i], 1, &nchars, f);
+ for (radix = true; !status && i < n->len; --i, radix = false)
+ status = bc_num_printHex(n->num[i], 1, radix, &nchars, f);
return status;
}
@@ -1415,36 +1399,30 @@
ptr = bc_vec_item_rev(&stack, i);
- status = print(*ptr, width, &nchars, f);
+ status = print(*ptr, width, false, &nchars, f);
if (status) goto frac_len_err;
}
if (!n->rdx) goto frac_len_err;
- status = bc_num_printRadix(&nchars, f);
-
- if (status) goto frac_len_err;
-
status = bc_num_init(&frac_len, n->len - n->rdx);
if (status) goto frac_len_err;
bc_num_one(&frac_len);
- while (frac_len.len <= n->len) {
-
- unsigned long fdigit;
+ for (radix = true; frac_len.len <= n->rdx; radix = false) {
status = bc_num_mul(&fracp, base, &fracp, n->rdx);
if (status) goto err;
- status = bc_num_ulong(&fracp, &fdigit);
+ status = bc_num_ulong(&fracp, &dig);
if (status) goto err;
- status = bc_num_ulong2num(&intp, fdigit);
+ status = bc_num_ulong2num(&intp, dig);
if (status) goto err;
@@ -1452,7 +1430,7 @@
if (status) goto err;
- status = print(fdigit, width, &nchars, f);
+ status = print(dig, width, radix, &nchars, f);
if (status) goto err;