@@ -301,12 +301,32 @@ static inline void bc_divide_copy_numerator(bc_num numerator, bc_num *num, size_
301
301
memcpy ((* num )-> n_value , numerator -> n_value , numerator -> n_len + scale );
302
302
}
303
303
304
- static inline void bc_divide_by_one (bc_num numerator , bc_num divisor , bc_num * quot , size_t quot_scale , bool use_quot )
304
+ static inline void bc_divide_by_one (
305
+ bc_num numerator , bc_num divisor , bc_num * quot , bc_num * rem ,
306
+ size_t quot_scale , size_t rem_scale , bool use_quot , bool use_rem )
305
307
{
306
308
if (use_quot ) {
307
309
bc_divide_copy_numerator (numerator , quot , quot_scale );
308
310
(* quot )-> n_sign = numerator -> n_sign == divisor -> n_sign ? PLUS : MINUS ;
309
311
}
312
+ if (use_rem ) {
313
+ /* When dividing by 1, the integer part of rem is always 0. */
314
+ rem_scale = MIN (numerator -> n_scale , rem_scale );
315
+ if (rem_scale == 0 ) {
316
+ * rem = bc_copy_num (BCG (_zero_ ));
317
+ } else {
318
+ * rem = bc_new_num_nonzeroed (1 , rem_scale ); /* 1 is for 0 */
319
+ (* rem )-> n_value [0 ] = 0 ;
320
+ /* copy fractional part */
321
+ memcpy ((* rem )-> n_value + 1 , numerator -> n_value + numerator -> n_len , rem_scale );
322
+ if (bc_is_zero (* rem )) {
323
+ (* rem )-> n_sign = PLUS ;
324
+ (* rem )-> n_scale = 0 ;
325
+ } else {
326
+ (* rem )-> n_sign = numerator -> n_sign ;
327
+ }
328
+ }
329
+ }
310
330
}
311
331
312
332
static inline void bc_divide_by_pow_10 (
@@ -361,7 +381,7 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
361
381
362
382
/* If divisor is 1 / -1, the quotient's n_value is equal to numerator's n_value. */
363
383
if (_bc_do_compare (divisor , BCG (_one_ ), divisor -> n_scale , false) == BCMATH_EQUAL ) {
364
- bc_divide_by_one (numerator , divisor , quot , quot_scale , use_quot );
384
+ bc_divide_by_one (numerator , divisor , quot , rem , quot_scale , rem_scale , use_quot , use_rem );
365
385
return true;
366
386
}
367
387
0 commit comments