void dec_exp(Decimal *result, const Decimal *a, unsigned int b)
{
Decimal tmp, power = *a;
dec_parse(result, "1");
while (b)
{
if (b & 1)
{
tmp = *result;
dec_mul(result, &tmp, &power);
}
if (b >>= 1)
{
tmp = power;
dec_mul(&power, &tmp, &tmp);
}
}
}
Where Decimal
is a structure variable containing a decimal value, it's length and the position where the decimal point exists.
Passed arguments in the function: a
is the base value and b
is power. And result
will store the value a^b after computation.
dec_parse(Decimal &x,string y)
will do a parsing of y
into decimal and extract info like the postion of decimal point, trim leading and trailing zeros and converts string into a Decimal
structure variable.
dec_mul(Decimal result, Decimal &x, Decimal &y)
will multiply x
and y
and store the value after multiplication in result.
I just want to know how two "if conditions" work in the while loop and when does the while loop get terminated and the time complexity of the snippet.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire