From 170a191b2bb63e43f710b1acdc1e36f9370ed033 Mon Sep 17 00:00:00 2001 From: GrieferAtWork Date: Sat, 28 Dec 2024 15:56:13 +0100 Subject: [PATCH] Fix leading 0es missing in fractional floats --- src/deemon/objects/float-print.c.inl | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/deemon/objects/float-print.c.inl b/src/deemon/objects/float-print.c.inl index a05109d5b..163a93a51 100644 --- a/src/deemon/objects/float-print.c.inl +++ b/src/deemon/objects/float-print.c.inl @@ -60,6 +60,7 @@ DeeFloat_Print(LOCAL_float_t value, dformatprinter printer, void *arg, bool is_negative; unsigned int max_prec, min_prec; uintmax_t whole, frac; + unsigned int frac_digits; /* Check for INF */ #ifdef CONFIG_HAVE_isinf @@ -104,6 +105,7 @@ DeeFloat_Print(LOCAL_float_t value, dformatprinter printer, void *arg, tmpval = (value - whole) * pow10[max_prec]; frac = (uintmax_t)tmpval; diff = tmpval - frac; + frac_digits = max_prec; /* Round to the closest fraction. */ if (diff > 0.5) { @@ -137,8 +139,10 @@ DeeFloat_Print(LOCAL_float_t value, dformatprinter printer, void *arg, /* Trim unused fraction digits. (should precision or * width require them, they'll be re-added later) */ - while (frac && (frac % 10) == 0) + while (frac_digits && (frac % 10) == 0) { frac /= 10; + --frac_digits; + } total_len = COMPILER_LENOF(buf) - len; if ((flags & (DEEFLOAT_PRINT_FWIDTH | DEEFLOAT_PRINT_FLJUST)) == /* */ (DEEFLOAT_PRINT_FWIDTH | DEEFLOAT_PRINT_FLJUST)) { @@ -148,16 +152,9 @@ DeeFloat_Print(LOCAL_float_t value, dformatprinter printer, void *arg, if (max_prec != 0) { unsigned int temp_min; temp_min = min_prec; - whole = frac; + if (temp_min < frac_digits) + temp_min = frac_digits; ++total_len; /* . */ - for (;;) { - if (temp_min) - --temp_min; - ++total_len; - whole /= 10; - if (!whole) - break; - } total_len += temp_min; } if (width <= total_len) @@ -199,18 +196,16 @@ do_float_normal_width: result += temp; /* Fractional part. */ - if (max_prec != 0) { + if (frac_digits != 0) { len = COMPILER_LENOF(buf); - for (;;) { + do { if (min_prec) --min_prec; buf[--len] = (char)(48 + (frac % 10)); frac /= 10; - if (!frac) - break; - } + } while (--frac_digits); buf[--len] = '.'; - temp = (*printer)(arg, buf + len, COMPILER_LENOF(buf) - len); + temp = (*printer)(arg, buf + len, COMPILER_LENOF(buf) - len); if unlikely(temp < 0) goto err; result += temp;