xxxxxxxxxx
public static int power(int base, int exponent) {
if (exponent == 0) {
return 1;
}
int result = base;
for (int i = 1; i < exponent; i++) {
result *= base;
}
return result;
}
xxxxxxxxxx
import java.lang.Math;
public class Main
{
public static void main(String[] args) {
int a = 2;
int b = 3;
double c = Math.pow(a, b);
System.out.println(c);
}
}
xxxxxxxxxx
import java.lang.Math;
public class _15 {
public static void main(String[] args) {
double num1 = 5;
double num2 = 2;
double powRes = Math.pow(num1, num2);
System.out.println(num1 + " ^ " + num2 + " = " + powRes);
}
}
xxxxxxxxxx
// this means 2 to the power of 4 - 2^4 - which will return 16
Math.pow(2, 4);
xxxxxxxxxx
// double Math.pow(double base, double exponent)
double twoCubed = Math.pow(2, 3);
System.out.println("Two raised to third power: " + twoCubed); // 8.0
xxxxxxxxxx
Method: Let x = 2 * (1+f)
1. Compute and return log2(x) in two pieces:
log2(x) = w1 + w2,
where w1 has 53-24 = 29 bit trailing zeros.
2. Perform y*log2(x) = n+y' by simulating muti-precision
arithmetic, where |y'|<=0.5.
3. Return x**y = 2**n*exp(y'*log2)