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

Answer from Marco13 on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ calculating-the-power-of-a-number-in-java-without-using-math-pow-method
Calculating the Power of a Number in Java Without Using Math pow() Method - GeeksforGeeks
March 2, 2021 - In Java, we can calculate the power of a number without using the predefined Math.pow() method. To calculate base^exponent, we need the base number and an exponent.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [java] calculate x^n without math.pow(x,n) and with an array?
r/learnprogramming on Reddit: [Java] Calculate X^n without Math.pow(x,n) and with an array?
January 9, 2017 -

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!

๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java numbers โ€บ calculating the power of any number in java without using math pow() method
Calculating the Power of Any Number in Java Without Using Math pow() Method | Baeldung
June 20, 2024 - A straightforward way to calculate the powะตr of a numbะตr is through itะตration. Here, weโ€™ll multiply the base by itsะตlf for the specified numbะตr of n times. A simple example: double result = 1; double base = 2; int exponent = 3; @Test ...
๐ŸŒ
Quora
quora.com โ€บ What-is-an-alternative-to-Math-pow-in-Java
What is an alternative to Math.pow in Java? - Quora
Answer (1 of 4): Yes you can write your function for this, [code]double power(double d, int n){ double p=1; for(int i=0;i
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [java] exponents without using math.pow?
r/learnprogramming on Reddit: [Java] Exponents without using Math.pow?
October 12, 2017 -

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?

๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ examples โ€บ power-number
Java Program to Calculate the Power of a Number
In this program, you'll learn to calculate the power of a number with and without using pow() function.
๐ŸŒ
Tech Support Guy
techguy.org โ€บ home โ€บ forums โ€บ software & hardware โ€บ software development
Exponents in Java without Math.pow | Tech Support Guy
Thank you, but I think I figured out a way to do it without a loop...Check it out {CODE} whenS = (initialDep * Math.exp((1 + interest) * Math.log(44))); whenSo = (initialDep * Math.exp((1 + interest) * Math.log(45))); interestWithdrawn = (((whenSo * interest) + whenSo) - whenSo); {CODE} ... Well you should've said I could use the Math class.
Find elsewhere
๐ŸŒ
Talkerscode
talkerscode.com โ€บ howto โ€บ java-exponents-without-math-pow.php
Java Exponents Without Math Pow
In order to calculate exponents in Java, a base number must be raised to a particular power. Developers can use iterative techniques, including using for or while loops, to accomplish repeated multiplication operations rather than relying on ...
๐ŸŒ
TestMu AI Community
community.testmu.ai โ€บ ask a question
How can I calculate the power of an integer in Java without using doubles? - TestMu AI Community
February 1, 2025 - Iโ€™m currently using Math.pow(a, b) to calculate powers in Java, but it returns a double, which makes my code less clean when I only need integer results. Is there a simple way to compute the power of 2 in Java (or any iโ€ฆ
๐ŸŒ
YouTube
youtube.com โ€บ shorts โ€บ Nq8kbckmkuE
How To Calculate Power Of A Number Without Using Math.Pow() Method in java | #java #coding - YouTube
This program tells you how to print power of a number without using math method #java#loop#forloop#coding#javacoding#javacode#code#programming#javaquestions#...
Published ย  April 7, 2022
๐ŸŒ
Carmatec
carmatec.com โ€บ home โ€บ java math.pow() explained: the java power function
Java Math.pow() Explained: The Java Power Function
January 7, 2026 - Math.pow(0.0, 0.0) returns 1.0 (by convention in Java). Any non-zero number raised to exponent 0.0 returns 1.0.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 662778 โ€บ java โ€บ power-Math-power-method-returning
power without Math.power -- method returning always 0.0 (Beginning Java forum at Coderanch)
Liutauras Vilda wrote:Also think about the case what the answer would be the number to the power of 0. That wouldn't work expected in your case. Here it was meant just for positive exponent, but sure case where exponent = 0 or <0 should be dealt with.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 408990 โ€บ java โ€บ Exponential-operator-java
Exponential operator in java? (Beginning Java forum at Coderanch)
As Fred Rosenberger has implied, there isn't an exponentiation operator as such, but if you stick to integers you can easily write a recursive algorithm to raise numbers to powers. Hint: If the power is an even number, halve it and square the result. That way your algorithm will run in O(logn) time.
๐ŸŒ
JanBask Training
janbasktraining.com โ€บ community โ€บ java โ€บ does-java-have-an-exponential-operator
Does Java have an exponential operator? | JanBask Training Community
May 13, 2025 - Java does not have a built-in exponential operator like ** (as in JavaScript or Python). So if youโ€™re wondering whether you can do something like 2 ** 3 in Java โ€” the answer is no.