@@ -311,12 +311,32 @@ static inline void bc_divide_copy_numerator(bc_num numerator, bc_num *num, size_
311
311
memcpy ((* num )-> n_value , numerator -> n_value , numerator -> n_len + scale );
312
312
}
313
313
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 )
315
317
{
316
318
if (use_quot ) {
317
319
bc_divide_copy_numerator (numerator , quot , quot_scale );
318
320
(* quot )-> n_sign = numerator -> n_sign == divisor -> n_sign ? PLUS : MINUS ;
319
321
}
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
+ }
320
340
}
321
341
322
342
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
371
391
372
392
/* If divisor is 1 / -1, the quotient's n_value is equal to numerator's n_value. */
373
393
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 );
375
395
return true;
376
396
}
377
397
0 commit comments