Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient 10 power x algorithm

Can anyone help me with finding an efficient code to find 10 power x?

 private int power(int base, int exp)
{
    int result = 1;
    while (exp != 0)
    {
        if ((exp & 1) == 1)
            result *= base;
        exp >>= 1;
        base *= base;
    }

    return result;
}

Source of code from here, but I am looking for a way where the input could be 3.14 (double). I also cannot use any library functions. The power can be a real number. So it is not just a simple integer algorithm where we can find by Exponentiation by Squaring.

like image 738
Sayan Avatar asked Jan 28 '26 18:01

Sayan


1 Answers

Why dont you use Math.pow(double, double)

you can even check out the source.


like image 176
Alexandre Santos Avatar answered Jan 31 '26 07:01

Alexandre Santos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!