×

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

Java Binary Operators

In this article, we are going to discuss about the binary operators in Java and their operations. Also, an example program will be discussed along with the operations. These are...

6 minutes read.

Access specifiers in Java

What are access specifiers in Java? Access specifiers in Java are used to determine whether other classes can use a particular field or invoke a particular method. Access specifiers mainly specify...

7 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

Star Pattern Programs in Java

Star Pattern Programs in Java The star pattern programs in Java is the part of pattern programs in Java, which we discussed earlier. Right Triangle Star Pattern Filename: StarPatternExample.java public class StarPatternExample {              public static void...

4 minutes read.

Constructor Program in Java

Constructor Program in Java In Java, a constructor is a piece of code that is used to create an object. A constructor is called implicitly when an object is created in...

7 minutes read.

Java Strings

String class is the most used class in Java programming language. The string is the sequence of characters, which is treated as objects in Java. Creating String objects We create String objects...

5 minutes read.

Group by in Java 8

By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this...

3 minutes read.

Majority Element in Java

It's an extremely intriguing question that is commonly asked in job interviews at prestigious IT firms like The Google, Amazon, TCS, and The Accenture, etc. By figuring out the solution, one may...

10 minutes read.

Association in Java

In Java, association refers to the link between two classes established by their objects. One-to-one, one-to-many, and many-to-many connections are managed via association. The Association defines the multiplicity between objects...

5 minutes read.

CopyOnWriteArrayList in Java

The CopyOnWriteArrayList is used to implement the List Interface. It is the improved version of ArrayList. The operations like add, remove, set, update etc, these operations are done by creating...

5 minutes read.

Class memory in Java

Anything specifically defined in the Java code is stored either inside the heap or the stack, which is particularly significant. Stack and heap are the two kinds of memory available...

6 minutes read.

How to Iterate List in Java?

List is a Collection foundation interface in Java. It enables us to keep the collection of objects in order. The four classes that make up the List interface implementation are...

3 minutes read.

Iterate JSON array in Java

This article is going to equip the knowledge about what JSON array is and how it works and also how is it distinct with the generic array. Json Array Ordered lists of...

4 minutes read.

Program to Find Square Root of a Number Without sqrt Method in Java

The Java Math class sqrt () function can be used to determine the square root of a number. In this section, we'll write a Java program to find a number's...

3 minutes read.

Java Queue Interface

Queue interface is a subtype of Collection interface. All methods in the Collection interface are also available in the Queue interface. It provides operations of Collection and also some additional...

2 minutes read.

Java Command Line Arguments

Java command line arguments Command line argument is an input or argument to the program when you run the program. In Java, we can take the inputs from the console, and...

2 minutes read.

Java Integer lowestOneBit()

The lowestOneBit () method of Java Integer class returns an int value with at most a single one-bit, in the position of the lowest-order one-bit in the specified int value.  Syntax public...

2 minutes read.

ConcurrentSkipListSet in Java

ConcurrentSkipListSet is a class that is a part of collection framework in Java.It has been available since Java version 1.6. ConcurrentSkipListSet  is a scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap.The...

16 minutes read.

Callable Statement in Java

The Callable statement in Java is used to call the functions and Stored procedures. Example: If we want to know about the age of a person based on their date of birth,...

3 minutes read.

Longest Odd Even Subsequence in Java

In order to solve the Java problem known as the longest odd-even subsequence, one must identify a sequences in a non-negative array having size s that alternately includes odd and...

6 minutes read.