×

Java Math pow() Method

The pow() method of Math class returns the value of first argument raised to the power of second argument i.e. ab.

Syntax:

public static double pow(double a, double b)

Parameters:

The parameters ‘a’ and ‘b’ represent the base and the exponent.

Return Value:

The pow() method computes the value of ab.

Special cases of the pow() method are as follows:

  • It returns 1.0, is second argument passed is positive or negative zero.
  • It returns the result same as the first argument, if the second argument is 1.0.
  • It returns NaN, is either argument is NaN.
  • It returns positive infinity, if ‘a’ is greater than 1 and ‘b’ is positive infinity or ‘a’ is less than 1 and ‘b’ is negative infinity.
  • It returns positive zero, if ‘a’ is greater than 1 and ‘b’ is negative infinity or ‘a’ is less than 1 and ‘b’ is positive infinity.
  • It returns NaN, if absolute value of the first argument is equal to 1 and second argument is infinite.
  • It returns positive zero, if ‘a’ is positive zero and ‘b’ is greater than zero or ‘a’ is positive infinity and ‘b’ is less than zero.
  • It returns positive infinity, if ‘a’ is positive zero and ‘b’ is less than zero or ‘a’ is positive infinity and ‘b’ is greater than zero.
  • It returns positive zero, if ‘a’ is negative zero and ‘b’ is greater than zero but not a finite odd integer or ‘a’ is negative infinity and ‘b’ is less than zero but not a finite odd integer.
  • It returns negative zero, if ‘a’ is negative zero and ‘b’ is positive finite odd integer or ‘a’ is negative infinity and ‘b’ is negative finite odd integer.
  • It returns positive infinity, if ‘a’ is negative zero and ‘b’ is less than zero but not a finite odd integer or ‘a’ is negative infinity and ‘b’ is greater than zero but not a finite odd integer.
  • It returns negative infinity, if ‘a’ is negative zero and ‘b’ is negative finite odd integer or ‘a’ is negative infinity and ‘b’ is positive finite odd integer.
  • It returns ab, if both the arguments are integers.

Example 1:

public class JavaMathPowExample1 {
    public static void main(String[] args) {
        double a = 2;
        double b = 8;
        // computes the value of a raised to the power b
        System.out.println(Math.pow(a,b));
    }
}

Output:

4.0

Example 2:

public class JavaMathPowExample2 {
    public static void main(String[] args) {
        double a = 2;
        double b = -0;
        // returns 1.0, is second argument passed is positive or negative zero.
        System.out.println(Math.pow(a,b));
    }
}

Output:

1.0

Example 3:

public class JavaMathPowExample3 {
    public static void main(String[] args) {
        double a = 2;
        double b = 1;
        // returns the result same as the first argument, if the second argument is 1.0
        System.out.println(Math.pow(a,b));
    }
}

Output:

2.0

Example 4:

public class JavaMathPowExample4 {
    public static void main(String[] args) {
        double a = Double.POSITIVE_INFINITY;
        double b = -1;
        /* returns positive zero, if ‘a’ is positive zero and ‘b’ is greater than zero
        or ‘a’ is positive infinity and ‘b’ is less than zero*/
        System.out.println(Math.pow(a,b));
    }
}

Output:

0.0

Example 5:

public class JavaMathPowExample5 {
    public static void main(String[] args) {
        double a = Double.NEGATIVE_INFINITY;
        double b = -77;
        /* returns negative zero, if ‘a’ is negative zero and ‘b’ is positive finite odd integer or
        ‘a’ is negative infinity and ‘b’ is negative finite odd integer*/
        System.out.println(Math.pow(a,b));
    }
}

Output:

-0.0

Related Topics

Console Errors in Java

An unlawful motion taken through the person that reasons this system to act abnormally is amistake until this system is compiled or run; maximum programming mistakes pass unnoticed.The software is...

3 minutes read.

Difference between final, finally, and finalize()

Difference between final, finally, and finalize() In Java, final, finally and finalize sound similar they are totally different in functionality. Final and finally are the two keywords but the finalize is...

4 minutes read.

Contextual keywords in Java

Contextual keywords were earlier known as restricted identifiers and restricted keywords. Context keywords are chosen based on their expected placement in the syntactic grammar. These are the keywords in the code...

3 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.

Sleeping Barber Problem in Java

The barbershop in this issue has one barber, one barber chair, and N chairs for customers in the waiting area. We may demonstrate the issue by keeping with the original...

6 minutes read.

Resultset in java

Resultset: A result set is an interface that is present in the package java.sql and the resultset is used to store the data that are returned from the database table after...

5 minutes read.

Java Naming Conventions

JAVA NAMING CONVENTIONS Java naming convention is a standard pattern for writing your identifier name such as class, interfaces, methods, constants, variable, etc. These patterns are not standard rules that you must...

2 minutes read.

Java Return Keyword

The return keyword in Java is used to end a method's execution. the caller receives the return, followed by the appropriate value. The return type of the method, such as...

3 minutes read.

How to compare three dates in Java?

While using the date and the time in Java, we occasionally have to compare the dates. Java does not compare dates the same way it compares the two numbers. Therefore,...

6 minutes read.

Dictionary in Java

Dictionary in Java The Dictionary class represents a key-value relation which maps keys to values. In Dictionary class, every key and every value is an object. It is an abstract class associated with...

2 minutes read.

Maximizing Profit in Stock Buy Sell in Java

In this tutorial, we will deal with a popular problem, a favourite of interviewers. The problem is named as Maximising profit in stock Buy Sell. we will see certain approaches...

6 minutes read.

Java String indexOf() method

Java String indexOf() method returns index of a given character or substring present in a String. Methods Description int indexOf(int ch)     It returns index position for the given char value.int indexOf(int ch,...

2 minutes read.

Java Pass-by-Reference

In Java, passing parameters can be done using one of two fundamental methods. Pass-by-value is used for the first and pass-by-reference is used for the second. One thing to keep...

2 minutes read.

Java delete directory

The File classes in Java may symbolize a directory or a file on the system. Inside the java.io package, the Files class is accessible. The File class has several helpful...

2 minutes read.

Java Program to Print Permutations of String

A string is given and you need to print all the possible ways for that string. Permutation is arranging the characters of a string to get outputs from the given...

3 minutes read.

Java Iterator

When iterating through, traversing, or retrieving the individual elements of a Collection or Flow object, a Java Cursor is leveraged as an iterator. In Java, there exist three types of...

4 minutes read.

Java List Interface

List interface is used when we have to order a collection which contains duplicate entries. Like an array, the elements of its implementation classes are retrieved and inserted at a...

2 minutes read.

Java Math ceil() Method

The ceil() method of Math class returns the smallest double value which is closest to negative infinity and is greater than or equal to the argument. Syntax: public static double ceil(double a) Parameters: The...

2 minutes read.

Manachers Algorithm in Java

Here, we'll go over the four scenarios once more in an effort to approach them differently and use the same strategy. The values of (centerRightPosition - currentRightPosition) and LPS length at...

3 minutes read.

Access modifiers in Java

Access modifiers in Java with Example In Java, there are two types of access modifiers one is a non-access modifier, and other is access modifier. If we talk about access modifier, there...

3 minutes read.