@@ -255,7 +255,8 @@ static inline void bc_standard_div(
255
255
static void bc_do_div (
256
256
const char * numerator , size_t numerator_size , size_t numerator_readable_size ,
257
257
const char * divisor , size_t divisor_size ,
258
- bc_num * quot , size_t quot_size
258
+ bc_num * quot , size_t quot_size ,
259
+ bool use_quot
259
260
) {
260
261
size_t numerator_arr_size = BC_ARR_SIZE_FROM_LEN (numerator_size );
261
262
size_t divisor_arr_size = BC_ARR_SIZE_FROM_LEN (divisor_size );
@@ -292,10 +293,11 @@ static void bc_do_div(
292
293
}
293
294
294
295
/* Convert to bc_num */
295
- char * qptr = (* quot )-> n_value ;
296
- char * qend = qptr + (* quot )-> n_len + (* quot )-> n_scale - 1 ;
297
-
298
- bc_convert_vector_to_char (quot_vectors , qptr , qend , quot_real_arr_size );
296
+ if (use_quot ) {
297
+ char * qptr = (* quot )-> n_value ;
298
+ char * qend = qptr + (* quot )-> n_len + (* quot )-> n_scale - 1 ;
299
+ bc_convert_vector_to_char (quot_vectors , qptr , qend , quot_real_arr_size );
300
+ }
299
301
300
302
if (allocation_arr_size > BC_STACK_VECTOR_SIZE ) {
301
303
efree (numerator_vectors );
@@ -309,33 +311,37 @@ static inline void bc_divide_copy_numerator(bc_num numerator, bc_num *num, size_
309
311
memcpy ((* num )-> n_value , numerator -> n_value , numerator -> n_len + scale );
310
312
}
311
313
312
- static inline void bc_divide_by_one (bc_num numerator , bc_num divisor , bc_num * quot , size_t quot_scale )
314
+ static inline void bc_divide_by_one (bc_num numerator , bc_num divisor , bc_num * quot , size_t quot_scale , bool use_quot )
313
315
{
314
- bc_divide_copy_numerator (numerator , quot , quot_scale );
315
- (* quot )-> n_sign = numerator -> n_sign == divisor -> n_sign ? PLUS : MINUS ;
316
+ if (use_quot ) {
317
+ bc_divide_copy_numerator (numerator , quot , quot_scale );
318
+ (* quot )-> n_sign = numerator -> n_sign == divisor -> n_sign ? PLUS : MINUS ;
319
+ }
316
320
}
317
321
318
322
static inline void bc_divide_by_pow_10 (
319
- const char * numeratorptr , size_t numerator_readable_size , bc_num * quot , size_t quot_size , size_t quot_scale )
323
+ const char * numeratorptr , size_t numerator_readable_size , bc_num * quot , size_t quot_size , size_t quot_scale , bool use_quot )
320
324
{
321
- char * qptr = (* quot )-> n_value ;
322
- for (size_t i = quot_size ; i <= quot_scale ; i ++ ) {
323
- * qptr ++ = 0 ;
324
- }
325
+ if (use_quot ) {
326
+ char * qptr = (* quot )-> n_value ;
327
+ for (size_t i = quot_size ; i <= quot_scale ; i ++ ) {
328
+ * qptr ++ = 0 ;
329
+ }
325
330
326
- size_t numerator_use_size = quot_size > numerator_readable_size ? numerator_readable_size : quot_size ;
327
- memcpy (qptr , numeratorptr , numerator_use_size );
328
- qptr += numerator_use_size ;
331
+ size_t numerator_use_size = quot_size > numerator_readable_size ? numerator_readable_size : quot_size ;
332
+ memcpy (qptr , numeratorptr , numerator_use_size );
333
+ qptr += numerator_use_size ;
329
334
330
- if (numerator_use_size < (* quot )-> n_len ) {
331
- /* e.g. 12.3 / 0.01 <=> 1230 */
332
- for (size_t i = numerator_use_size ; i < (* quot )-> n_len ; i ++ ) {
333
- * qptr ++ = 0 ;
335
+ if (numerator_use_size < (* quot )-> n_len ) {
336
+ /* e.g. 12.3 / 0.01 <=> 1230 */
337
+ for (size_t i = numerator_use_size ; i < (* quot )-> n_len ; i ++ ) {
338
+ * qptr ++ = 0 ;
339
+ }
340
+ (* quot )-> n_scale = 0 ;
341
+ } else {
342
+ char * qend = (* quot )-> n_value + (* quot )-> n_len + (* quot )-> n_scale ;
343
+ (* quot )-> n_scale -= qend - qptr ;
334
344
}
335
- (* quot )-> n_scale = 0 ;
336
- } else {
337
- char * qend = (* quot )-> n_value + (* quot )-> n_len + (* quot )-> n_scale ;
338
- (* quot )-> n_scale -= qend - qptr ;
339
345
}
340
346
}
341
347
@@ -358,7 +364,7 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
358
364
359
365
/* If divisor is 1 / -1, the quotient's n_value is equal to numerator's n_value. */
360
366
if (_bc_do_compare (divisor , BCG (_one_ ), divisor -> n_scale , false) == BCMATH_EQUAL ) {
361
- bc_divide_by_one (numerator , divisor , quot , quot_scale );
367
+ bc_divide_by_one (numerator , divisor , quot , quot_scale , use_quot );
362
368
return true;
363
369
}
364
370
0 commit comments