Java scalb Function

The Java scalb Function is one of the Java Math Library functions, which is to return x * 2ScaleFactor. The scalb Function result rounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set. In this article, we will show how to use Math.scalb function in Java Programming language with example.

Java scalb Function syntax

The basic syntax of the Math.scalb Function in Java Programming language is as shown below:

Math.scalb(data_type number, int ScaleFactor);
  • number: Please specify the double value or specify any expression here.
  • ScaleFactor: Please specify the Scale Factor value here.

For example, if number = 2 and ScaleFactor = 3 then, Math.scalb(3, 2) = 3 * 2² = 12. There are some special cases where the result will be different, and they are:

  • If the first argument is infinite, Math.scalb will return the result as Infinity of the same sign.
  • If the first argument is positive or negative zero, Math.scalb will return the result as Zero of the same sign.
  • And if the first argument is not a Number, Java Math.scalb will return the result as NaN.

Java Programming provides two different Math.scalb Java functions to return number * 2ScaleFactor. The following function will accept positive or negative double value as the first argument and returns the result

static double scalb(double number, int ScaleFactor); //Return Type is double

// In order to use in program: 
Math.scalb(double number, int ScaleFactor);

The following function will accept positive or negative float value as the first argument and returns the result

static float scalb(float number, int ScaleFactor);//Return Type is float

// In order to use in program: 
Math.scalb(float number, int ScaleFactor);

Java scalb Function Example

The Java Math.scalb function returns number * 2ScaleFactor. In this Java example, We are going to find the same of different data types and display the output

// Java Math.scalb Function
package MathFunctions;

public class ScalbMethod {
	public static void main(String[] args) {
		double a = 10.9666, b = 14.6674, c = -9.474, d = -14.9865;
		float e = 1.23f, f = 4.809f, g = -7.89f, h = -6.54f;

		System.out.println("\nMath.scalb Result for Zero as Second argument = " + Math.scalb(2, 0));
		System.out.println("Math.scalb Result for Zero as First argument = " + Math.scalb(0, 2));

		System.out.println("\nMath.scalb Result of Positive Value = " + Math.scalb(2, 3));		
		System.out.println("Math.scalb Result of Negative Value = " + Math.scalb(-3, 5));
		System.out.println("Math.scalb Result of Negative Value = " + Math.scalb(3, -4));
		System.out.println("Math.scalb Result of Negative Value = " + Math.scalb(-2, -2));
		
		System.out.println("\nMath.scalb Result of Positive Doubles: " + Math.scalb(a, 4));
		System.out.println("Math.scalb Result of Negative Double Values: " + Math.scalb(c, 3));
		System.out.println("Math.scalb Result of both Positive & Negative Double Values: " + Math.scalb(b, -3));
		System.out.println("Math.scalb Result of both Positive & Negative Double Values: " + Math.scalb(d, -6));
		
		System.out.println("\nMath.scalb Result of Positive Float Values: " + Math.scalb(e, 5));
		System.out.println("Math.scalb Result of Negative Float Values: " + Math.scalb(g, 7));
		System.out.println("Math.scalb Result of both Positive & Negative Float Values: " + Math.scalb(f, -4));
		System.out.println("Math.scalb Result of both Positive & Negative Float Values: " + Math.scalb(h, -2));
	}
}
Java scalb Function 1

First, we used the Math.scalb Function directly on both the Positive and negative values.

System.out.println("\nMath.scalb Result of Positive Value = " + Math.scalb(2, 3));		
System.out.println("Math.scalb Result of Negative Value = " + Math.scalb(-3, 5));
System.out.println("Math.scalb Result of Negative Value = " + Math.scalb(3, -4));
System.out.println("Math.scalb Result of Negative Value = " + Math.scalb(-2, -2));

Next, we used the Java Math.scalb Function on variable a, b, c, and d (they belongs to double type). The following statements will call the scalb method of double type ( static double scalb(double number, int ScaleFactor) ) to display the result.

System.out.println("\nMath.scalb Result of Positive Doubles: " + Math.scalb(a, 4));
System.out.println("Math.scalb Result of Negative Double Values: " + Math.scalb(c, 3));
System.out.println("Math.scalb Result of both Positive & Negative Double Values: " + Math.scalb(b, -3));
System.out.println("Math.scalb Result of both Positive & Negative Double Values: " + Math.scalb(d, -6));

Here, We used the scalb Math function on variable e, f, g, and h (they belong to float type). The following statements will call the scalb method of float type ( static float scalb(float number, int ScaleFactor) ) to display the result.

System.out.println("\nMath.scalb Result of Positive Float Values: " + Math.scalb(e, 5));
System.out.println("Math.scalb Result of Negative Float Values: " + Math.scalb(g, 7));
System.out.println("Math.scalb Result of both Positive & Negative Float Values: " + Math.scalb(f, -4));
System.out.println("Math.scalb Result of both Positive & Negative Float Values: " + Math.scalb(h, -2));