A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/math-pow-method-in-java-with-example/ below:

Math pow() Method in Java with Example

Math pow() Method in Java with Example

Last Updated : 28 Mar, 2025

The Math.pow() method in Java is used to calculate a number raised to the power of some other number. It is part of the java.lang.Math class and is widely used in mathematical computations, scientific calculations, and algorithmic problems. This function accepts two parameters and returns the value of first parameter raised to the second parameter.

Example 1: This example demonstrates how to use the Math.pow() method in Java to calculate the power of a number (base raised to the exponent).

Java
// Java program to demonstrate working
// of Math.pow() method

// importing java.lang package
import java.lang.Math;

class Geeks {

    // driver code
    public static void main(String args[])
    {
        double a = 30;
        double b = 2;
        System.out.println(Math.pow(a, b));

        a = 3;
        b = 4;
        System.out.println(Math.pow(a, b));

        a = 2.5;
        b = 6.9;
        System.out.println(Math.pow(a, b));
    }
}

Output
900.0
81.0
556.9113382296638

Explanation: In the above code, we have declared two variables a (base) and b (exponent) and then the program calculates the result of raising the base (a) to the power of the exponent (b) using the Math.pow() method and prints the result.

Syntax of Math.pow() Method

The method signature is listed below:

public static double pow(double base, double exponent)

Parameter:

Return Types: This method return a floating-point number (e.g., 8.0, 2.5, NaN).

Key features and Special Cases

The below table demonstrates the special cases:

Cases

Result

exponent = 0

1.0 (any number⁰ = 1)

exponent = 1

Same as base (e.g., 5¹ = 5)

exponent = NaN

NaN (Not a Number)

base = 0, exponent < 0

Infinity

base = NaN

NaN

Important Points:

Example 2: This example demonstrates how the Math.pow() method handles special cases, such as NaN, 0, and 1 as exponents.

Java
// handling different cases Nan, 0 and 1 as exponents.

// importing java.lang package
import java.lang.Math; 
public class GFG {
    public static void main(String[] args)
    {

        double nan = Double.NaN;
        double result;

        // Here second argument is NaN,
        // output will be NaN
        result = Math.pow(2, nan);
        System.out.println(result);

        // Here second argument is zero
        result = Math.pow(1254, 0);
        System.out.println(result);

        // Here second argument is one
        result = Math.pow(5, 1);
        System.out.println(result);
    }
}

Explanation: The above code handles special cases in exponentiation such as When the exponent is NaN, the result is NaN, When the exponent is 0, the result is 1.0 (except for base 0), When the exponent is 1, the result is the base itself.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4