Skip to content

Commit 3ba28d6

Browse files
committed
Optimized the generation of initial values.
1 parent 684e6d2 commit 3ba28d6

File tree

1 file changed

+5
-7
lines changed
  • ext/bcmath/libbcmath/src

1 file changed

+5
-7
lines changed

ext/bcmath/libbcmath/src/sqrt.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,12 @@ bool bc_sqrt(bc_num *num, size_t scale)
7676
cscale = (*num)->n_scale;
7777
} else {
7878
/* The number is greater than 1. Guess should start at 10^(exp/2). */
79-
bc_init_num(&guess);
80-
bc_int2num(&guess, 10);
79+
/* If just divide size_t by 2 it will not overflow. */
80+
size_t exponent_for_initial_guess = (size_t) (*num)->n_len >> 1;
8181

82-
bc_int2num(&guess1, (*num)->n_len);
83-
bc_multiply_ex(guess1, point5, &guess1, 0);
84-
guess1->n_scale = 0;
85-
bc_raise_bc_exponent(guess, guess1, &guess, 0);
86-
bc_free_num (&guess1);
82+
/* 10^n is a 1 followed by n zeros. */
83+
guess = bc_new_num(exponent_for_initial_guess + 1, 0);
84+
guess->n_value[0] = 1;
8785
cscale = 3;
8886
}
8987

0 commit comments

Comments
 (0)