Java Math.pow() method Usage – Coding N Concepts



Java Math.pow() method Usage – Coding N Concepts | ninjasquad

In this quick article, We’ll see usage of Java Math.pow() method which takes two arguments, a and b, and returns a to the power of b i.e. ab

Syntax

public double pow(double a, double b)

Where,

  • Parameter a is the base
  • Parameter b is the exponent
  • Returns ab

Example

// Returns 8.0
double result = (int) Math.pow(2, 3);

// Cast to int, returns 8
int intResult = (int) Math.pow(2, 3);

// Returns 117.29730800599916
double doubleResult = Math.pow(2.5, 5.2);

// Returns 1.0
double zeroPowerResult = Math.pow(2, 0);

// Returns 2.0
double sameResult = Math.pow(2, 1);

// Returns NaN
double nanResult = Math.pow(2, Double.NaN);

Points to note:-

  1. By default, returns the result in double
  2. You can cast the result to int
  3. Both arguments, base and exponent can have decimal points
  4. If second argument is zero then result will be 1.0
  5. If second argument is 1 then result will be value of first argument
  6. If second argument is NaN then result will be NaN



Source: Internet

Leave a Comment

We are offering free coding tuts

X