Java Tutorial Java Advanced Java Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Java - Math scalb() Method



The Java Math scalb() method returns a x 2b. If the exponent of the result is between Double.MIN_EXPONENT/Float.MIN_EXPONENT and Double.MAX_EXPONENT/Float.MAX_EXPONENT, the result is calculated exactly. In special cases it returns the following:

  • If the first argument is NaN, NaN is returned.
  • If the first argument is infinite, then an infinity of the same sign is returned.
  • If the first argument is zero, then a zero of the same sign is returned.

Syntax

public static double scalb(double a, int b)
public static float scalb(float a, int b)

Parameters

a Specify value to be scaled by power of 2.
b Specify power of 2 used to scale a.

Return Value

Returns a x 2b.

Exception

NA.

Example:

In the example below, scalb() method returns a x 2b.

public class MyClass {
 public static void main(String[] args) {
  System.out.println(Math.scalb(2.55, 4)); 
  System.out.println(Math.scalb(10, 3));   
 }
}

The output of the above code will be:

40.8
80.0

❮ Java Math Methods

5