Data structures & algorithms (Beginner to Intermediate)

Free guided path13 chapters99+ problems

Earn badges and level up

Introduction

Understanding the Math.pow() function in Java can be challenging for many developers. However, fear not – we're here to assist you in navigating the intricacies of this function, which can be likened to a robust calculator, simplifying complex mathematical operations and offering a versatile tool for various tasks.

In many languages, the inbuilt power function is not so powerful, but it can handle several edge cases without failing in Java. Let's discuss them in detail.

What is Power Function in Java?

pow() function in Java is used to raise one number to the power of another. It takes two arguments: the base and the exponent, and returns the result as a double, with the syntax double result = Math. pow(base, exponent.

The power function in Java returns the value of the first argument raised to the power of the second argument. It takes two arguments as parameters, and by default, it returns data type double. Unlike other programming languages, the power function in Java works almost like actual maths operations as it is not just limited to finding exponents. They can handle negative and decimal numbers as well.

Signature

public static double pow(double base, double exponent)

Syntax

Math.pow(a,b)

The syntax Math.pow(a, b) refers to a function where a and b are arguments. a is the base number, and b is the exponent. The function calculates and returns the value of a raised to the power of b. For example, if a is 2 and b is 3, it calculates

Explanation

The above function takes two arguments, usually of double data type. The above function returns base^exponent as output. Here a is the base, and b is the exponent. Its time complexity is O(log b), and space complexity is O(1).

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

How Do I Use the power Function in Java?

In Java, the power function is used through `Math.pow(double a, double b)`, where `a` is the base and `b` is the exponent. It returns `a` raised to the power of `b`. For example, `Math.pow(2, 3)` returns 8.0, as it computes (2^{3}).

Example of Power function in Java

In the below example, we have shown some different combinations of base and exponents for better understanding.

Code

public class NinjaPower {
public static void main(String[] args) {
// Case 1
double base = 2;
double exponent = 3;
double result = Math.pow(base, exponent);
System.out.println(base + " to the power of " + exponent + " is " + result);
// Case 2
base = 2;
exponent = 0.5;
result = Math.pow(base, exponent);
System.out.println(base + " to the power of " + exponent + " is " + result);
// Case 3
base = 0.2;
exponent = 3;
result = Math.pow(base, exponent);
System.out.println(base + " to the power of " + exponent + " is " + result);
}
}

Output

Some Special Cases of Power Function in Java

There are some peculiar cases when the argument has different values. Let’s discuss them:

Second Argument as Zero

If the second argument is a positive or negative zero, then the power function returns 1.0.

Code

class CodingNinja {
public static void main(String[] args) {
double base = 5.0;
double exponent = -0;
double result = Math.pow(base, exponent);
System.out.println(base + " to the power of " + exponent + " is " + result);
}
}

Output

Second Argument as NaN

If the second argument is not a number (NaN), then the power function also returns NaN.

Code

class CodingNinja {
public static void main(String[] args) {
double base = 5.0;
double exponent = Double.NaN;
double result = Math.pow(base, exponent);
System.out.println(base + " to the power of " + exponent + " is " + result);
}
}

Output

Second Argument as Negative

If the second argument is a negative number, then the power function solves it just like general maths, i.e., a^(-x) becomes (1/a)^x.

Code

class CodingNinja {
public static void main(String[] args) {
double base = 5.0;
double exponent = -2;
double result = Math.pow(base, exponent);
System.out.println(base + " to the power of " + exponent + " is " + result);
}
}

Output

How to Return Integer type Value Using the Power Function

To return an integer value using the power function in Java, first use `Math.pow()` to compute the power, which returns a double, and then cast the result to an integer. For example, `(int) Math.pow(2, 3)` calculates (2^{3}) as 8.0 and then casts it to 8, an integer.

Example :

public class Main {
public static void main(String[] args) {
int base = 2;
int exponent = 3;
int result = (int) Math.pow(base, exponent); // Calculates 2^3 and casts the result to an integer
System.out.println("Result: " + result); // Outputs: Result: 8
}
}

In this code, Math.pow(2, 3) computes 2323 and returns 8.0 as a double. The (int) before Math.pow() casts this double to an integer, making result equal to 8.

Frequently Asked Questions

How do you write 2 power n in Java?

In Java, to write 2 raised to the power of n, you use `Math.pow(2, n)`. This function takes 2 as the base and n as the exponent, computing and returning (2^{n}). For example, `Math.pow(2, 3)` returns 8.0.

What does Math POW () return?

The `Math.pow()` function in Java returns a double value representing the result of raising the first argument (base) to the power of the second argument (exponent). For example, `Math.pow(2, 3)` returns 8.0, which is (2^{3}).

What is Math pow with 3 arguments?

The Python `pow()` function with three arguments, `pow(base, exponent, modulus)`, computes the power and then applies the modulus, returning the remainder of the power divided by the modulus. This is useful for modular exponentiation, especially in cryptography.

Is there any alternative to the power function in Java?

We can use the exponentiation operator (**) or implement the power function using a loop. The Math.pow() method is generally the simplest and most efficient way to perform power functions in Java.

Conclusion

In this article, we discussed the Power function in Java. We also saw different examples and some peculiar cases and their output. The power function is provided in almost all major programming languages, but they should be coded correctly, and the edge cases should be kept in mind.

But suppose you are just a beginner and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. For placement preparations, you must look at the problems, interview experiences, and interview bundles.