Java Math Pow Method

JavaJavaBeginner
Practice Now

Introduction

The pow() method is used to find the value of a number raised to the power of some other number. It is part of the Math class in Java and returns a double value. It is important to note that explicit casting is required if we want to convert the output to some other type. In this lab, we will learn how to use the pow() method with various examples.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/math("`Math`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/type_casting("`Type Casting`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/math_methods("`Math Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117939{{"`Java Math Pow Method`"}} java/classes_objects -.-> lab-117939{{"`Java Math Pow Method`"}} java/class_methods -.-> lab-117939{{"`Java Math Pow Method`"}} java/modifiers -.-> lab-117939{{"`Java Math Pow Method`"}} java/oop -.-> lab-117939{{"`Java Math Pow Method`"}} java/packages_api -.-> lab-117939{{"`Java Math Pow Method`"}} java/wrapper_classes -.-> lab-117939{{"`Java Math Pow Method`"}} java/identifier -.-> lab-117939{{"`Java Math Pow Method`"}} java/arrays -.-> lab-117939{{"`Java Math Pow Method`"}} java/comments -.-> lab-117939{{"`Java Math Pow Method`"}} java/data_types -.-> lab-117939{{"`Java Math Pow Method`"}} java/math -.-> lab-117939{{"`Java Math Pow Method`"}} java/operators -.-> lab-117939{{"`Java Math Pow Method`"}} java/output -.-> lab-117939{{"`Java Math Pow Method`"}} java/strings -.-> lab-117939{{"`Java Math Pow Method`"}} java/type_casting -.-> lab-117939{{"`Java Math Pow Method`"}} java/variables -.-> lab-117939{{"`Java Math Pow Method`"}} java/math_methods -.-> lab-117939{{"`Java Math Pow Method`"}} java/system_methods -.-> lab-117939{{"`Java Math Pow Method`"}} end

Setting Up

Create a new file named PowDemo.java in the ~/project directory and open it in a text editor.

Add the code below to the file and save it.

import java.lang.Math;

public class PowDemo {
    public static void main(String[] args) {

    }
}

To run the code, open a terminal, cd into the ~/project directory, and enter the following commands:

javac PowDemo.java
java PowDemo

Using the pow() method

To use the pow() method, call the Math.pow() function and pass in the base and power values as arguments. The general signature of the method is shown below.

public static double pow(double base, double power)

Add the following code to the main function after the import statement.

double d1 = Math.pow(3, 5);
double d2 = Math.pow(3.5, 1.5);
int i = (int) Math.pow(3.5, 1.5); // require casting for int type

System.out.println("3 raised to the power 5: " + d1);
System.out.println("3.5 raised to the power 1.5: " + d2);
System.out.print("3.5 raised to the power 1.5(for int type): " + i);

This block of code computes the result of 3 raised to the power of 5, 3.5 raised to the power of 1.5, and 3.5 raised to the power of 1.5 for an integer value, and prints out the values to the console.

Finding Square Roots and Cube Roots

The pow() method can also be used to find the square roots or cube roots of numbers. In this step, we will use the pow() method to find the square root of 16, and the cube root of 27.

Add the following code to the main function after the code you wrote in Step 2.

double squareRoot = Math.pow(16, 0.5);
double cubeRoot = Math.pow(27, (1.0 / 3));

System.out.println("Square root of 16 is: " + squareRoot);
System.out.print("Cube root of 27 is: " + cubeRoot);

This block of code computes the square root of 16 and cube root of 27 using the pow() method and prints out the results to the console.

Handling Special Cases

The pow() method has a few special cases that are worth mentioning. In this step, we will test for some of these special cases.

Add the following code to the main function after the code you wrote in Step 3.

System.out.println("When Second parameter is zero:");
System.out.println(Math.pow(5.0, 0.0));
System.out.println(Math.pow(5.0, -0.0));

System.out.println("When Second parameter is one:");
System.out.println(Math.pow(5.0, 1));

System.out.println("When Second parameter is NaN:");
System.out.println(Math.pow(5.0, Double.NaN));

System.out.println("When first parameter is NaN and second parameter is non-zero:");
System.out.println(Math.pow(Double.NaN, 14));

System.out.println("When first parameter is NaN and second parameter is zero:");
System.out.println(Math.pow(Double.NaN, 0));

This block of code tests for the following special cases:

  • When the second parameter is zero, and it returns 1.0
  • When the second parameter is one, and it returns the first parameter
  • When the second parameter is NaN, and it returns NaN
  • When the first parameter is NaN and the second parameter is non-zero, and it returns NaN
  • When the first parameter is NaN but the second parameter is zero, and it returns 1

Testing Advanced Mathematical Concepts

In this step, we will test for some advanced mathematical concepts using the pow() method.

Add the following code to the main function after the code you wrote in Step 4.

System.out.print("Square root of -1: " + Math.pow(-1, 0.5));
System.out.println("Positive Value raised to Infinity: " + Math.pow(5, Double.POSITIVE_INFINITY));
System.out.print("Positive Value raised to Negative Infinity: " + Math.pow(5, Double.NEGATIVE_INFINITY));

This block of code tests for the following cases:

  • When the square root of -1 is computed, and it returns NaN.
  • When a positive value is raised to infinity, and it returns positive infinity.
  • When a positive value is raised to negative infinity, and it returns 0.

Save and Run the Code

Save the changes you made to the PowDemo.java. Open a terminal, cd into the ~/project directory, and enter the following commands to run the code:

javac PowDemo.java
java PowDemo

This will compile and run the the PowDemo.java. The output of the program should be as follows:

3 raised to the power 5: 243.0
3.5 raised to the power 1.5: 6.547900426854397
3.5 raised to the power 1.5(for int type): 6
Square root of 16 is: 4.0
Cube root of 27 is: 3.0
When Second parameter is zero:
1.0
1.0
When Second parameter is one:
5.0
When Second parameter is NaN:
NaN
When first parameter is NaN and second parameter is non-zero:
NaN
When first parameter is NaN and second parameter is zero:
1.0
Square root of -1: NaN
Positive Value raised to Infinity: Infinity
Positive Value raised to Negative Infinity: 0.0

Summary

In this lab, we learned how to use the pow() method of the java.lang.Math class in Java. We also looked at some special cases that may occur when using this method. The pow() method is important in computing algorithms, especially when computing advanced mathematical concepts.

Other Java Tutorials you may like