Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

Java scalb Function

by suresh

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));
	}
}

OUTPUT

Java scalb Function 1

ANALYSIS

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));

Placed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy