Skip to content

Commit

Permalink
Fix leading 0es missing in fractional floats
Browse files Browse the repository at this point in the history
  • Loading branch information
GrieferAtWork committed Dec 28, 2024
1 parent fd4ea92 commit 170a191
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/deemon/objects/float-print.c.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 170a191

Please sign in to comment.