Home » Java programming language

Java Math class scalb() method with example

Math class scalb() method in Java: Here, we are going to learn about the scalb() method of Math class in Java programming language with its syntax and example.
Submitted by Preeti Jain, on September 16, 2019

Math class scalb() method

  • scalb() method is available in java.lang package.
  • scalb() method is used to return the a* 2 raised to the power of scale rounded as an argument as passed in the method. Here, a is the first parameter and scale is the second parameter.
  • scalb() method is a static method, it is accessible with the class name too.
  • scalb() method does not throw any exception.

Syntax:

    public static float scalb(float a , int sf)
    public static double scalb(double a , int sf);

Parameter(s):

  • a – represents number to be scaled by the power of 2.
  • sf (Scale Factor) – represents number power of 2 used to scale a.

Return value:

The return type of this method is float/double, – it returns the a*2 raised to the power of sf.

Note:

  • If we pass "NaN", it returns the same value (i.e. "NaN").
  • If we pass "Double.MIN_EXPONENT" / "Float.MIN_EXPONENT" or "Double.MAX_EXPONENT" / "Float.MAX_EXPONENT", it returns the same value.
  • If we pass "Double.MAX_EXPONENT" / "Float.MAX_EXPONENT", it returns the infinity.
  • If we pass infinity as the first argument, it returns the same value with the same sign.
  • If we pass zero (0) as the first argument, it returns the same value with the same sign.

Java program to demonstrate example of scalb() method

// Java program to demonstrate the example of 
// scalb(double do , int sf) method of Math Class

public class ScalbMethod {
    public static void main(String[] args) {
        // declaring the variables
        double d1 = -0.0;
        double d2 = -7.0 / 0.0;
        double d3 = 20.0;
        int i = 6;

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

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

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

Output

E:\Programs>javac ScalbMethod.java
E:\Programs>java ScalbMethod
Math.scalb(d1,i): -0.0
Math.scalb(d2,i): -Infinity
Math.scalb(d3,i): -Infinity

Example 2:

// Java program to demonstrate the example of 
// scalb(float fi , int sf) method of Math Class

public class ScalbMethod {
    public static void main(String[] args) {
        // declaring the variables
        float f1 = -0.0f;
        float f2 = -7.0f / 0.0f;
        float f3 = 20.0f;
        int i = 6;

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

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

        // Here , we will get (20.0 * 2 raised to the power of 6.0) 
        // because are passing parameters whose value is (20.0,6)
        System.out.println("Math.scalb(f3,i): " + Math.scalb(f2, i));
    }
}

Output

E:\Programs>javac ScalbMethod.java
E:\Programs>java ScalbMethod
Math.scalb(d1,i): -0.0
Math.scalb(d2,i): -Infinity
Math.scalb(d3,i): -Infinity


Comments and Discussions!

Load comments ↻





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