@@ -301,12 +301,32 @@ static inline void bc_divide_copy_numerator(bc_num numerator, bc_num *num, size_
301301 memcpy ((* num )-> n_value , numerator -> n_value , numerator -> n_len + scale );
302302}
303303
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 )
305307{
306308 if (use_quot ) {
307309 bc_divide_copy_numerator (numerator , quot , quot_scale );
308310 (* quot )-> n_sign = numerator -> n_sign == divisor -> n_sign ? PLUS : MINUS ;
309311 }
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+ }
310330}
311331
312332static 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
361381
362382 /* If divisor is 1 / -1, the quotient's n_value is equal to numerator's n_value. */
363383 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 );
365385 return true;
366386 }
367387
0 commit comments