Java Math scalb() Method
The scalb() method of Java Math class returns a single perfectly rounded product of floating-point and member of the double value set as if performed by d*2scaleFacor rounded value.
Syntax:
- public static double scalb(double d, int scaleFactor)
- public static float scalb(float d,int scaleFactor)
Parameters:
The parameter ‘d’ represents the number to be scaled by a power of 2 and ‘scaleFactor’ represents power of 2 used to scale d.
Return Value:
The scalb() method computes the rounded value for d*2scaleFacor.
Example 1:
public class JavaMathScalbExample1 { static int i=1; public static void main(String[] args) { double d=0.5d; int scaleFactor=9; //returns a single perfectly rounded product of floating-point System.out.println(i++ +". Value : " +Math.scalb(d,scaleFactor)); } }
Output:
Value : 256.0
Example 2:
public class JavaMathScalbExample2 { static int i=1; public static void main(String[] args) { double d=2; int scaleFactor=0; //returns d if the scaleFactor's value is 0 System.out.println(i++ +". Value : " +Math.scalb(d,scaleFactor)); } }
Output:
Value : 2.0
Example 3:
public class JavaMathScalbExample3 { static int i=1; public static void main(String[] args) { double d=Double.NaN; int scaleFactor=Integer.MIN_VALUE; //returns NaN, if either argument is NaN System.out.println(i++ +". Value : " +Math.scalb(d,scaleFactor)); } }
Output:
Value : NaN
Example 4:
public class JavaMathScalbExample4{ static int i=1; public static void main(String[] args) { double d=Double.POSITIVE_INFINITY; int scaleFactor=1; //returns Infinity, if d is Infinite System.out.println(i++ +". Value : " +Math.scalb(d,scaleFactor)); } }
Output:
Value : Infinity
Example 5:
public class JavaMathScalbExample5 { static int i=1; public static void main(String[] args) { float d=Float.NEGATIVE_INFINITY; int scaleFactor=1; //returns NEGATIVE Infinity, if d is NEGATIVE Infinite System.out.println(i++ +". Value : " +Math.scalb(d,scaleFactor)); } }
Output:
Value : -Infinity