1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
public class PowX { public double myPow(double x, int n) { boolean isPositive = n > 0; double result = 1.0; n = n > 0 ? n : -n; while (n > 0) { if ((n & 1) == 1) { result *= x; } x *= x; n = n >> 1; } return isPositive ? result : (1.0 / result); } }
|