
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Math scalb(float a, int b) method
Description
The Java Math scalb(float f,int scaleFactor) returns f x 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set. See the Java Language Specification for a discussion of floating-point value sets. If the exponent of the result is between Float.MIN_EXPONENT and Float.MAX_EXPONENT, the answer is calculated exactly. If the exponent of the result would be larger than Float.MAX_EXPONENT, an infinity is returned. Note that if the result is subnormal, precision may be lost; that is, when scalb(x, n) is subnormal, scalb(scalb(x, n), -n) may not equal x. When the result is non-NaN, the result has the same sign as f.
If the first argument is NaN, NaN is returned.
If the first argument is infinite, then an infinity of the same sign is returned.
If the first argument is zero, then a zero of the same sign is returned.
Declaration
Following is the declaration for java.lang.Math.scalb() method
public static float scalb(float f, int scaleFactor)
Parameters
f − number to be scaled by a power of two.
scaleFactor − power of 2 used to scale d
Return Value
This method returns f x 2scaleFactor
Exception
NA
Example: Getting scalb for Positive Float Value
The following example shows the usage of Math scalb() method for positive values.
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get a float number float x = 2.0f; int y = 5; // print x raised by y and then y raised by x System.out.println("Math.scalb(" + x + "," + y + ")=" + Math.scalb(x, y)); } }
Output
Let us compile and run the above program, this will produce the following result −
Math.scalb(2.0,5)=64.0
Example: Getting scalb for Negative Float Value
The following example shows the usage of Math scalb() method for negative value.
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get a float number float x = -2.0f; int y = 5; // print x raised by y and then y raised by x System.out.println("Math.scalb(" + x + "," + y + ")=" + Math.scalb(x, y)); } }
Output
Let us compile and run the above program, this will produce the following result −
Math.scalb(-2.0,5)=-64.0
Example: Getting scalb for Negative Float Value
The following example shows the usage of Math scalb() method for negative scale factor.
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get a float number float x = 2.0f; int y = -5; // print x raised by y and then y raised by x System.out.println("Math.scalb(" + x + "," + y + ")=" + Math.scalb(x, y)); } }
Output
Let us compile and run the above program, this will produce the following result −
Math.scalb(2.0,-5)=0.0625