Skip to content

Commit c7916b5

Browse files
committed
Using rem in bc_divide_by_pow_10
1 parent 2d63079 commit c7916b5

File tree

1 file changed

+26
-2
lines changed
  • ext/bcmath/libbcmath/src

1 file changed

+26
-2
lines changed

ext/bcmath/libbcmath/src/div.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,10 @@ static inline void bc_divide_by_one(
340340
}
341341

342342
static inline void bc_divide_by_pow_10(
343-
const char *numeratorptr, size_t numerator_readable_size, bc_num *quot, size_t quot_size, size_t quot_scale, bool use_quot)
343+
const char *numeratorptr, size_t numerator_readable_size, size_t numerator_leading_zeros,
344+
bc_num *quot, size_t quot_size, size_t quot_scale, bool use_quot,
345+
bc_num *rem, size_t rem_size, bool use_rem,
346+
size_t numerator_rem_len_diff)
344347
{
345348
if (use_quot) {
346349
char *qptr = (*quot)->n_value;
@@ -363,6 +366,23 @@ static inline void bc_divide_by_pow_10(
363366
(*quot)->n_scale -= qend - qptr;
364367
}
365368
}
369+
if (use_rem) {
370+
size_t rem_leading_zeros = numerator_leading_zeros + quot_size - numerator_rem_len_diff;
371+
if (rem_size <= rem_leading_zeros) {
372+
bc_free_num(rem);
373+
*rem = bc_copy_num(BCG(_zero_));
374+
return;
375+
}
376+
/* The values after this have already been copied, so just need to set them to 0. */
377+
for (size_t i = 0; i < rem_leading_zeros; i++) {
378+
(*rem)->n_value[i] = 0;
379+
}
380+
_bc_rm_leading_zeros(*rem);
381+
if (bc_is_zero(*rem)) {
382+
(*rem)->n_sign = PLUS;
383+
(*rem)->n_scale = 0;
384+
}
385+
}
366386
}
367387

368388
bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, size_t scale, bool use_quot, bool use_rem)
@@ -492,7 +512,11 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
492512

493513
/* If divisor is 1 here, return the result of adjusting the decimal point position of numerator. */
494514
if (divisor_size == 1 && *divisorptr == 1) {
495-
bc_divide_by_pow_10(numeratorptr, numerator_readable_size, quot, quot_size, quot_scale);
515+
bc_divide_by_pow_10(
516+
numeratorptr, numerator_readable_size, numerator_leading_zeros,
517+
quot, quot_size, quot_scale, use_quot,
518+
rem, rem_size, use_rem, numerator_rem_len_diff
519+
);
496520
return true;
497521
}
498522

0 commit comments

Comments
 (0)