Powers of 2 can simply be computed by Bit Shift Operators
int exponent = ...
int powerOf2 = 1 << exponent;
Even for the more general form, you should not compute an exponent by "multiplying n times". Instead, you could do Exponentiation by squaring
Powers of 2 can simply be computed by Bit Shift Operators
int exponent = ...
int powerOf2 = 1 << exponent;
Even for the more general form, you should not compute an exponent by "multiplying n times". Instead, you could do Exponentiation by squaring
Here is a post that allows both negative/positive power calculations.
https://stackoverflow.com/a/23003962/3538289
Function to handle +/- exponents with O(log(n)) complexity.
double power(double x, int n){
if(n==0)
return 1;
if(n<0){
x = 1.0/x;
n = -n;
}
double ret = power(x,n/2);
ret = ret * ret;
if(n%2!=0)
ret = ret * x;
return ret;
}
Videos
I've been trying to calculate the Xn without using math.pow in order to compare it with other algorithms to test their performance and math.pow is too optimized so it will impede me to compare it successfully with other algorithms:
I will use the Xn method to use it within a bruteforce method that evaluates a polynomial at x.
This method will be compared with my horners method.
If I do it using math.pow(x,n) the bruteforce will be faster than horners sometimes and I presume that its because math.pow being too fast.
Here are both method to compare, can you shed some light on why sometimes bruteforce is faster than horners, is it because of math.pow?
//Metodo de Horner:
public double horners(int[] coef_array, double x) {
double resultado = 0;
int c = 0;
for (int i = coef_array.length - 1; i >= 0; i--) {
resultado = (resultado * x) + coef_array[i];
c++;
} //Evalua en x;
set_cant_mult_H(c);
return resultado;
}
//Metodo "obvio", "a pie" o "bruteforceado":
public double brute(int[] coef_array, double x) {
double resultado = 0;
int c = 0;
for (int i = coef_array.length - 1; i >= 0; i--) {
c++;
resultado += coef_array[i] * Math.pow(x, i); // Change this mathpow with something slower?!
}
set_cant_mult_BF(c);
return resultado;
} So to wrap it off, Horners bruteforce are compared but sometimes horners is slower than bruteforce and I presume its because math.pow(x,n) is too fast.
Remember I am using arrays so maybe the new pow method may need to receive an array!
I'm doing a project for my first year programming class in which I'm to write a program that approximates the square root of a number (without using Math.sqrt), and the only method I'm allowed to use is the absolute value method.
To handle the margin of error, I'm to use the equation |m2 - n| <= epsilon, where n = a user entered number, m = the midpoint of the range, and epsilon = 1E-10. My roommate, a 4th year math major, says that the epsilon equation is simply 1-10 (a great relief, because I had no idea what that E was supposed to be). I'm unsure of how to handle the exponent without being able to use the Math.pow method.
Could anybody grant me any insight?
The case with b<0 only makes sense with floating point numbers, so I changed the type of a and the return value to double.
public static double mathPower(double a, int b)
{
double result = 1;
if (b < 0) {
a = 1.0 / a;
b = -b;
}
for (int i = 0; i < b; i++) {
result = result * a;
}
return result;
}
You have three main problems:
The
returnstatement inside the loop is breaking it in the first repetition.You're using your
avariable as the loop variable.If you allow negative exponents then the return value should be a double.
public static double mathPower(double a, int b)
{
double result = 1.0;
if (b == 0)
{
result = 1.0;
}
if (b < 0)
{
a = (1.0 / a);
b = -b;
}
for (int i = 0; i < b; i++)
{
result = result * a;
}
return result;
}
You or a program you wrote may be suffering from Integer Overflow.
This is caused by chronic limitation of the int type.
Symptoms include
Negative integers that are really supposed to be positive
Small numbers that are supposed to be big
This condition can be controlled by ensuring that your int values don't exceed 2 billion.
If symptoms persist, see a debugger, or print out intermediate values.
*side effects may include frustration, throwing your computer out of a window, and/or deleting important system files.
But in all reality, let's say that you have a base of seven.
7=7
7*7=49
49*7=343
The last digit is 3.
However, if you, only take the last digit in between operations,
7*7 =49 -> 9
9*7 =63
The last digit is still three.
Doing this keeps the number well below the int limit.
This is actually what the p=(p*m)%10; solution is:
p= (p*m) %10
multiply the previous digit by the exponent take the last digit
The int variable is overflowing. Try changing p=p*m to p=(p*m)%10.
I've been asked to create a method that takes two numbers, one for the base and one for the exponent, and then calculate and return the result. How can I go about this without utilizing the Math.pow() ?
There is no operator, but there is a method.
Math.pow(2, 3) // 8.0
Math.pow(3, 2) // 9.0
FYI, a common mistake is to assume 2 ^ 3 is 2 to the 3rd power. It is not. The caret is a valid operator in Java (and similar languages), but it is binary xor.
To do this with user input:
public static void getPow(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter first integer: "); // 3
int first = sc.nextInt();
System.out.println("Enter second integer: "); // 2
int second = sc.nextInt();
System.out.println(first + " to the power of " + second + " is " +
(int) Math.pow(first, second)); // outputs 9