Skip to content

Commit 484d9df

Browse files
committed
Using rem in bc_divide_by_one
1 parent b949c5e commit 484d9df

File tree

1 file changed

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

1 file changed

+22
-2
lines changed

ext/bcmath/libbcmath/src/div.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,32 @@ static inline void bc_divide_copy_numerator(bc_num numerator, bc_num *num, size_
311311
memcpy((*num)->n_value, numerator->n_value, numerator->n_len + scale);
312312
}
313313

314-
static inline void bc_divide_by_one(bc_num numerator, bc_num divisor, bc_num *quot, size_t quot_scale, bool use_quot)
314+
static inline void bc_divide_by_one(
315+
bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem,
316+
size_t quot_scale, size_t rem_scale, bool use_quot, bool use_rem)
315317
{
316318
if (use_quot) {
317319
bc_divide_copy_numerator(numerator, quot, quot_scale);
318320
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
319321
}
322+
if (use_rem) {
323+
/* When dividing by 1, the integer part of rem is always 0. */
324+
rem_scale = MIN(numerator->n_scale, rem_scale);
325+
if (rem_scale == 0) {
326+
*rem = bc_copy_num(BCG(_zero_));
327+
} else {
328+
*rem = bc_new_num_nonzeroed(1, rem_scale); /* 1 is for 0 */
329+
(*rem)->n_value[0] = 0;
330+
/* copy fractional part */
331+
memcpy((*rem)->n_value + 1, numerator->n_value + numerator->n_len, rem_scale);
332+
if (bc_is_zero(*rem)) {
333+
(*rem)->n_sign = PLUS;
334+
(*rem)->n_scale = 0;
335+
} else {
336+
(*rem)->n_sign = numerator->n_sign;
337+
}
338+
}
339+
}
320340
}
321341

322342
static inline void bc_divide_by_pow_10(
@@ -371,7 +391,7 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
371391

372392
/* If divisor is 1 / -1, the quotient's n_value is equal to numerator's n_value. */
373393
if (_bc_do_compare(divisor, BCG(_one_), divisor->n_scale, false) == BCMATH_EQUAL) {
374-
bc_divide_by_one(numerator, divisor, quot, quot_scale, use_quot);
394+
bc_divide_by_one(numerator, divisor, quot, rem, quot_scale, rem_scale, use_quot, use_rem);
375395
return true;
376396
}
377397

0 commit comments

Comments
 (0)