@@ -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 = (numerator_size + BC_VECTOR_SIZE - 1 ) / BC_VECTOR_SIZE ;
261
262
size_t divisor_arr_size = (divisor_size + BC_VECTOR_SIZE - 1 ) / BC_VECTOR_SIZE ;
@@ -284,9 +285,11 @@ static void bc_do_div(
284
285
}
285
286
286
287
/* Convert to bc_num */
287
- char * qptr = (* quot )-> n_value ;
288
- char * qend = qptr + (* quot )-> n_len + (* quot )-> n_scale - 1 ;
289
- bc_convert_vector_to_char (qptr , qend , quot_vectors , quot_real_arr_size );
288
+ if (use_quot ) {
289
+ char * qptr = (* quot )-> n_value ;
290
+ char * qend = qptr + (* quot )-> n_len + (* quot )-> n_scale - 1 ;
291
+ bc_convert_vector_to_char (qptr , qend , quot_vectors , quot_real_arr_size );
292
+ }
290
293
291
294
efree (numerator_vectors );
292
295
}
@@ -298,33 +301,37 @@ static inline void bc_divide_copy_numerator(bc_num numerator, bc_num *num, size_
298
301
memcpy ((* num )-> n_value , numerator -> n_value , numerator -> n_len + scale );
299
302
}
300
303
301
- static inline void bc_divide_by_one (bc_num numerator , bc_num divisor , bc_num * quot , size_t quot_scale )
304
+ static inline void bc_divide_by_one (bc_num numerator , bc_num divisor , bc_num * quot , size_t quot_scale , bool use_quot )
302
305
{
303
- bc_divide_copy_numerator (numerator , quot , quot_scale );
304
- (* quot )-> n_sign = numerator -> n_sign == divisor -> n_sign ? PLUS : MINUS ;
306
+ if (use_quot ) {
307
+ bc_divide_copy_numerator (numerator , quot , quot_scale );
308
+ (* quot )-> n_sign = numerator -> n_sign == divisor -> n_sign ? PLUS : MINUS ;
309
+ }
305
310
}
306
311
307
312
static inline void bc_divide_by_pow_10 (
308
- const char * numeratorptr , size_t numerator_readable_size , bc_num * quot , size_t quot_size , size_t quot_scale )
313
+ const char * numeratorptr , size_t numerator_readable_size , bc_num * quot , size_t quot_size , size_t quot_scale , bool use_quot )
309
314
{
310
- char * qptr = (* quot )-> n_value ;
311
- for (size_t i = quot_size ; i <= quot_scale ; i ++ ) {
312
- * qptr ++ = 0 ;
313
- }
315
+ if (use_quot ) {
316
+ char * qptr = (* quot )-> n_value ;
317
+ for (size_t i = quot_size ; i <= quot_scale ; i ++ ) {
318
+ * qptr ++ = 0 ;
319
+ }
314
320
315
- size_t numerator_use_size = quot_size > numerator_readable_size ? numerator_readable_size : quot_size ;
316
- memcpy (qptr , numeratorptr , numerator_use_size );
317
- qptr += numerator_use_size ;
321
+ size_t numerator_use_size = quot_size > numerator_readable_size ? numerator_readable_size : quot_size ;
322
+ memcpy (qptr , numeratorptr , numerator_use_size );
323
+ qptr += numerator_use_size ;
318
324
319
- if (numerator_use_size < (* quot )-> n_len ) {
320
- /* e.g. 12.3 / 0.01 <=> 1230 */
321
- for (size_t i = numerator_use_size ; i < (* quot )-> n_len ; i ++ ) {
322
- * qptr ++ = 0 ;
325
+ if (numerator_use_size < (* quot )-> n_len ) {
326
+ /* e.g. 12.3 / 0.01 <=> 1230 */
327
+ for (size_t i = numerator_use_size ; i < (* quot )-> n_len ; i ++ ) {
328
+ * qptr ++ = 0 ;
329
+ }
330
+ (* quot )-> n_scale = 0 ;
331
+ } else {
332
+ char * qend = (* quot )-> n_value + (* quot )-> n_len + (* quot )-> n_scale ;
333
+ (* quot )-> n_scale -= qend - qptr ;
323
334
}
324
- (* quot )-> n_scale = 0 ;
325
- } else {
326
- char * qend = (* quot )-> n_value + (* quot )-> n_len + (* quot )-> n_scale ;
327
- (* quot )-> n_scale -= qend - qptr ;
328
335
}
329
336
}
330
337
@@ -347,7 +354,7 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
347
354
348
355
/* If divisor is 1 / -1, the quotient's n_value is equal to numerator's n_value. */
349
356
if (_bc_do_compare (divisor , BCG (_one_ ), divisor -> n_scale , false) == BCMATH_EQUAL ) {
350
- bc_divide_by_one (numerator , divisor , quot , quot_scale );
357
+ bc_divide_by_one (numerator , divisor , quot , quot_scale , use_quot );
351
358
return true;
352
359
}
353
360
0 commit comments