Saturday, August 30, 2025
HomeLanguagesJavaMath scalb() Method in Java

Math scalb() Method in Java

  1. The scalb(double a, int scale ) is an inbuilt method of Math class in Java which is used to get the value a x 2^scale . The result is accurately calculated when the exponent of the result is between Double.MIN_EXPONENT and Double.MAX_EXPONENT. It gives rise to four special results:
    • It returns an infinity when the exponent of the result is larger than Double.MAX_EXPONENT.
    • The result is NaN when the first argument is NaN.
    • The result is an infinity of the same sign when the first argument is infinite.
    • It returns a Zero of the same sign when the first argument is zero.

    Syntax :

    public static double scalb(double a, int scale)

    Parameters : This method accepts two parameters they are:

    • a: This is of the double type which is the number to be scaled by a power of two.
    • scale: This is of integer type which is the power of 2 ,used to scale a

    Return Value : The method returns a x 2^scale
    Examples :

    Input: 
    a = 77.23
    scale = 3
    
    Output = 617.84
    

    Below program illustrates the java.lang.Math.scalb(double a, int scale) method:




    // Java praogram to illustrate the
    // java.lang.Math.scalb(double a, int scale )
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            double p = 52.12;
            int scale = 8;
      
            // It returns p x 2^scale
            System.out.print("Value of Math.scalb(" 
                         + p + ", " + scale + ") = ");
            System.out.println(Math.scalb(p, scale));
        }
    }

    
    
    Output:

    Value of Math.scalb(52.12, 8) = 13342.72
    
  2. The java.lang.Math.scalb(float a, int scale ) is an inbuilt method which returns a x 2^scale . The result is accurately calculated when the exponent of the result is between Float.EXPONENT and Float.MAX_EXPONENT.
    • It returns an infinity when the exponent of the result is larger than Float.MAX_EXPONENT.
    • The result is NaN when the first argument is NaN.
    • The result is an infinity of the same sign when the first argument is infinite.
    • It returns a Zero of the same sign when the first argument is zero.

    Syntax :

    public static double scalb(float a, int scale)

    Parameters : This method accepts two parameters:

    • a: This is of float type which is the number to be scaled by a power of two.
    • scale: This is of integer type which refers to the power of 2 used in scaling of a

    Return Value : The method returns a x 2^scale
    Examples :

    Input: 
    a = 32.14f
    scale = 6
    
    Output = 2056.96
    

    Below program illustrates the java.lang.Math.scalb(float a, int scale ) method:
    Program 1:




    // Java praogram to illustrate the
    // java.lang.Math.scalb(float a, int scale )
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            float p = 81.27f;
            int scale = 8;
      
            // Calculate p multiplied by 2 raised in scale
            System.out.print("Value of Math.scalb(" +
                                p + ", " + scale + ") = ");
            System.out.println(Math.scalb(p, scale));
        }
    }

    
    
    Output:

    Value of Math.scalb(81.27, 8) = 20805.12
    
RELATED ARTICLES

Most Popular

Dominic
32250 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11840 POSTS0 COMMENTS
Shaida Kate Naidoo
6732 POSTS0 COMMENTS
Ted Musemwa
7014 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6704 POSTS0 COMMENTS