Skip to content

Commit 68bcd84

Browse files
committed
Using rem in bc_divide_by_pow_10
1 parent 67b28bc commit 68bcd84

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
@@ -330,7 +330,10 @@ 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_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, size_t rem_size, bool use_rem,
336+
size_t numerator_rem_len_diff)
334337
{
335338
if (use_quot) {
336339
char *qptr = (*quot)->n_value;
@@ -353,6 +356,23 @@ static inline void bc_divide_by_pow_10(
353356
(*quot)->n_scale -= qend - qptr;
354357
}
355358
}
359+
if (use_rem) {
360+
size_t rem_leading_zeros = numerator_leading_zeros + quot_size - numerator_rem_len_diff;
361+
if (rem_size <= rem_leading_zeros) {
362+
bc_free_num(rem);
363+
*rem = bc_copy_num(BCG(_zero_));
364+
return;
365+
}
366+
/* The values after this have already been copied, so just need to set them to 0. */
367+
for (size_t i = 0; i < rem_leading_zeros; i++) {
368+
(*rem)->n_value[i] = 0;
369+
}
370+
_bc_rm_leading_zeros(*rem);
371+
if (bc_is_zero(*rem)) {
372+
(*rem)->n_sign = PLUS;
373+
(*rem)->n_scale = 0;
374+
}
375+
}
356376
}
357377

358378
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)
@@ -484,7 +504,11 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
484504

485505
/* If divisor is 1 here, return the result of adjusting the decimal point position of numerator. */
486506
if (divisor_size == 1 && *divisorptr == 1) {
487-
bc_divide_by_pow_10(numeratorptr, numerator_readable_size, quot, quot_size, quot_scale);
507+
bc_divide_by_pow_10(
508+
numeratorptr, numerator_readable_size, numerator_leading_zeros,
509+
quot, quot_size, quot_scale, use_quot,
510+
rem, rem_size, use_rem, numerator_rem_len_diff
511+
);
488512
return true;
489513
}
490514

0 commit comments

Comments
 (0)