In Java, Math.scalb(d, n) returns d × 2n. Math.scalb(3.0, 4) is 3 × 16 = 48.0, and a negative n divides: Math.scalb(1.0, -3) is 0.125.
Because a double is stored as "digits times a power of two", multiplying by a power of two only needs the exponent to move, like shifting a decimal point. That makes scalb exact whenever the result stays in the normal range. It is the floating-point cousin of the integer shift 1 << n, and the same idea as ldexp in C.
A static method of the java.lang.Math class, added in Java 6 (JDK 1.6). Call it on the class name, as in Math.scalb(...). No import is needed, because classes in java.lang are always visible.
Picture it
In decimal, multiplying 3.7 by 1000 just moves the point: 3700. In binary, multiplying by 2n does the same thing to a double's binary point. No digits change, so nothing is rounded.
Syntax
Pass the number and an int power of two. The result has the type of the first argument.
Java
double a = Math.scalb(3.0, 4); // 48.0 (3 * 2^4)
double b = Math.scalb(1.0, -3); // 0.125 (1 / 2^3)
float c = Math.scalb(1.5f, 3); // 12.0 (float version)
double same = 3.0 * Math.pow(2, 4); // 48.0, but via a general power
Call
What it does
Math.scalb(x, 1)
Doubles x.
Math.scalb(x, -1)
Halves x.
Math.scalb(1.0, n)
Exactly 2n as a double.
Math.scalb(1.0, 1024)
Too big for a double: returns Infinity.
The name comes from "scale binary". The second argument is always an int; there is no version with a fractional power. An int first argument widens to float, so Math.scalb(3, 4) returns the float 48.0.
Library declarations
Java
// java.lang.Math
public static double scalb(double d, int scaleFactor)
public static float scalb(float f, int scaleFactor)
Parameters
Parameter
Description
d / f
The number to scale.
scaleFactor
The power of two to multiply by. Positive values make the number bigger, negative values smaller.
Return Value
Return Value
Description
double / float
d × 2scaleFactor, rounded once like a single multiplication. Too large gives Infinity (with the sign of d); too small gives a tiny subnormal value or zero. NaN, infinities, and zeros are returned unchanged.
Positive n multiplies. 3.0 × 24 = 3 × 16 = 48.0, and 5.5 × 22 = 22.0. The sign is kept, so -1.5 × 8 is -12.0.
Negative n divides. 2-3 is 1/8, so scalb(1.0, -3) is 0.125, exact because 0.125 is a power of two.
Same value as pow, less work.Math.pow(2, 4) is exactly 16.0, so both lines agree; scalb just moves the exponent. The 1.5f call uses the float overload and returns a float.
Example 2 — Double the Tile Width at Every Zoom Level
Online maps double their size with each zoom level. scalb scales a 256-pixel tile by 2level, also for negative levels.
Past the top exponent. The largest double is just under 21024, so 21024 itself overflows to Infinity.
The bottom edge. 2-1074 is exactly Double.MIN_VALUE, 4.9E-324. 2-1075 is exactly halfway between that and zero; the tie goes to the "even" value, 0.0.
Zero, NaN, and the round trip. Zero times anything stays 0.0 and NaN stays NaN. Scaling 0.1 up by 250 and back down only moves the exponent twice, so the original double returns unchanged.
Frequently Asked Questions
It multiplies a number by 2 to the power of the second argument: Math.scalb(3.0, 4) is 3 times 16, which is 48.0. A negative power divides instead.
Math.pow computes any power, for example 3 to the 2.5. Math.scalb only multiplies by a whole power of two, which it does by moving the exponent, so it is simpler and exact for normal results.
It is the floating-point version of the idea. 1 left-shifted by n multiplies an int by 2 to the n, but wraps around for large n; scalb works on doubles, accepts negative n, and overflows to Infinity instead of wrapping.
It returns Infinity, or -Infinity for a negative number. Results that are too small become tiny subnormal values or 0.0. It never throws an exception.
Java 6 (JDK 1.6), for both the float and the double version.