Skip to content

Commit b6d7903

Browse files
committed
Using rem in bc_divide_by_pow_10
1 parent 1559297 commit b6d7903

File tree

1 file changed

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

1 file changed

+25
-2
lines changed

ext/bcmath/libbcmath/src/div.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,9 @@ static inline void bc_divide_by_one(
330330
}
331331

332332
static inline void bc_divide_by_pow_10(
333-
const char *numeratorptr, size_t numerator_readable_size, bc_num *quot, size_t quot_size, size_t quot_scale, bool use_quot)
333+
const char *numeratorptr, size_t numerator_len, size_t numerator_readable_size, size_t numerator_leading_zeros,
334+
bc_num *quot, size_t quot_size, size_t quot_scale, bool use_quot,
335+
bc_num *rem, bool use_rem)
334336
{
335337
if (use_quot) {
336338
char *qptr = (*quot)->n_value;
@@ -353,6 +355,23 @@ static inline void bc_divide_by_pow_10(
353355
(*quot)->n_scale -= qend - qptr;
354356
}
355357
}
358+
if (use_rem) {
359+
size_t rem_leading_zeros = numerator_leading_zeros + quot_size - (numerator_len - (*rem)->n_len);
360+
if ((*rem)->n_len + (*rem)->n_scale <= rem_leading_zeros) {
361+
bc_free_num(rem);
362+
*rem = bc_copy_num(BCG(_zero_));
363+
return;
364+
}
365+
/* The values after this have already been copied, so just need to set them to 0. */
366+
for (size_t i = 0; i < rem_leading_zeros; i++) {
367+
(*rem)->n_value[i] = 0;
368+
}
369+
_bc_rm_leading_zeros(*rem);
370+
if (bc_is_zero(*rem)) {
371+
(*rem)->n_sign = PLUS;
372+
(*rem)->n_scale = 0;
373+
}
374+
}
356375
}
357376

358377
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)
@@ -481,7 +500,11 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
481500

482501
/* If divisor is 1 here, return the result of adjusting the decimal point position of numerator. */
483502
if (divisor_size == 1 && *divisorptr == 1) {
484-
bc_divide_by_pow_10(numeratorptr, numerator_readable_size, quot, quot_size, quot_scale);
503+
bc_divide_by_pow_10(
504+
numeratorptr, numerator->n_len, numerator_readable_size, numerator_leading_zeros,
505+
quot, quot_size, quot_scale, use_quot,
506+
rem, use_rem
507+
);
485508
return true;
486509
}
487510

0 commit comments

Comments
 (0)