Home » Java programming language

Java StrictMath scalb() Method with Example

StrictMath Class scalb() method: Here, we are going to learn about the scalb() method of StrictMath Class with its syntax and example.
Submitted by Preeti Jain, on January 05, 2020

StrictMath Class scalb() method

Syntax:

    public static double scalb(double do , int sf);
    public static float scalb(float fl , int sf);
  • scalb(double do , int sf) method is used to return do* 2 raised to the power of sf rounded as an argument as passed in the method.
  • scalb(double do , int sf)
    • If the exponent lies in between Double.MIN_EXPONENT and Double.MAX_EXPONENT and in that case the result is exactly calculated.
    • If the exponent answer would be greater than Double.MAX_EXPONENT and in that case infinity is returned.
    • scalb(float fl , int sf) method is used to return fl* 2 raised to the power of sf rounded as an argument as passed in the method.
  • scalb(float fl , int sf)
    • If the exponent lies in between Float.MIN_EXPONENT and Float.MAX_EXPONENT and in that case the result is exactly calculated.
    • If the exponent answer would be greater than Float.MAX_EXPONENT and in that case infinity is returned.
  • These methods don't throw an exception.
  • These are static methods, it is accessible with the class name and, if we try to access these methods with the class object then we will not get any error.

Parameter(s):

  • In the first case, scalb(double do , int sf) -
    • double do - This parameter represents the double number to be scaled by the power of 2.
    • int sf - This argument represents integer type number power of 2 used to scale do.
  • In the second case, scalb(float fl , int sf) -
    • float fl - This argument represents the float type number to be scaled by the power of 2.
    • int sf - This argument represents integer number power of 2 used to scale fl.

Return value:

In the first case, the return type of this method is double it returns the do* 2 raised to the power of sf.

In the second case, the return type of this method is float it returns the fl* 2 raised to the power of sf.

Example:

// Java program to demonstrate the example 
// of scalb () method of StrictMath class

public class Scalb {
    public static void main(String[] args) {
        // variable declarations
        float f1 = -0.0f;
        float f2 = -7.0f / 0.0f;
        float f3 = 20.0f;
        double d1 = -0.0;
        double d2 = -7.0 / 0.0;
        double d3 = 20.0;
        int i = 6;

        System.out.println("scalb(double do, int i): ");

        // Here , we will get (-0.0) because we are passing
        // parameter whose value is (-0.0f,6)
        System.out.println("StrictMath.scalb( f1,i): " + StrictMath.scalb(f1, i));

        // Here , we will get (-Infinity) and we are passing parameter 
        // whose value is (-Infinity,6)
        System.out.println("StrictMath.scalb( f2,i): " + StrictMath.scalb(f2, i));

        // Here , we will get (20.0f * 2 raised to the power of 6) and
        // we are passing parameter whose value is (20.0f,6)
        System.out.println("StrictMath.scalb( f3,i): " + StrictMath.scalb(f3, i));

        System.out.println();
        System.out.println("scalb(double do, int i): ");

        // Here , we will get (-0.0) because we are passing 
        // parameter whose value is (-0.0,6)
        System.out.println("StrictMath.scalb( d1,i): " + StrictMath.scalb(d1, i));

        // Here , we will get (-Infinity) and we are passing parameter 
        // whose value is (-Infinity,6)
        System.out.println("StrictMath.scalb( d2,i): " + StrictMath.scalb(d2, i));

        // Here , we will get (20.0 * 2 raised to the power of 6.0) and
        // we are passing parameter whose value is (20.0,6)
        System.out.println("StrictMath.scalb( d3,i): " + StrictMath.scalb(d3, i));
    }
}

Output

scalb(double do, int i): 
StrictMath.scalb( f1,i): -0.0
StrictMath.scalb( f2,i): -Infinity
StrictMath.scalb( f3,i): 1280.0

scalb(double do, int i): 
StrictMath.scalb( d1,i): -0.0
StrictMath.scalb( d2,i): -Infinity
StrictMath.scalb( d3,i): 1280.0



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.