Java Math scalb() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 6+

Definition

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
CallWhat 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

ParameterDescription
d / fThe number to scale.
scaleFactorThe power of two to multiply by. Positive values make the number bigger, negative values smaller.

Return Value

Return ValueDescription
double / floatd × 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.

Compare with Similar Methods

Other ways to multiply by a power of two:

MethodHow it differs
x * pow(2, n)Same value for these cases, but computes a general power first.
1 << nInteger shift: only for int/long, wraps around past 31 or 63.
getExponent(x)Reads the exponent that scalb moves.
x * 8.0Fine for a fixed power; scalb takes the power as a variable.

Java Examples

Example 1 — Scale Up, Scale Down, and the float Version

Multiply and divide by powers of two, then check the result against Math.pow.

Java
public class MathScalbBasics {
    public static void main(String[] args) {
        System.out.println("scalb(3.0, 4)  = " + Math.scalb(3.0, 4));
        System.out.println("scalb(1.0, -3) = " + Math.scalb(1.0, -3));
        System.out.println("scalb(5.5, 2)  = " + Math.scalb(5.5, 2));
        System.out.println("scalb(-1.5, 3) = " + Math.scalb(-1.5, 3));
        System.out.println("scalb(1.5f, 3) = " + Math.scalb(1.5f, 3));
        System.out.println("same as 3.0 * pow(2, 4)? " + (Math.scalb(3.0, 4) == 3.0 * Math.pow(2, 4)));
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Java
public class MathScalbZoom {
    public static void main(String[] args) {
        double tile = 256.0;
        for (int level = 0; level <= 3; level++) {
            double width = Math.scalb(tile, level);
            System.out.println("zoom " + level + ": " + width + " px, same as 256 << " + level + "? " + (width == (256 << level)));
        }
        System.out.println("zoom -2: " + Math.scalb(tile, -2) + " px");
    }
}

How It Works

  1. Each level doubles the width. 256 × 20, 21, 22, 23 gives 256.0, 512.0, 1024.0, and 2048.0 pixels.
  2. The integer shift agrees. 256 << level also multiplies by 2level; Java widens that int to double for the == test, which prints true every time.
  3. Zooming out works too. A shift cannot use a negative count to divide, but Math.scalb(256.0, -2) simply divides by 4 and gives 64.0.

Example 3 — Too Big, Too Small, and an Exact Round Trip

See where scalb overflows to Infinity or underflows to zero, and prove that ordinary scaling loses nothing.

Java
public class MathScalbLimits {
    public static void main(String[] args) {
        System.out.println("scalb(1.0, 1024)  = " + Math.scalb(1.0, 1024));
        System.out.println("scalb(1.0, -1074) = " + Math.scalb(1.0, -1074));
        System.out.println("scalb(1.0, -1075) = " + Math.scalb(1.0, -1075));
        System.out.println("scalb(0.0, 100)   = " + Math.scalb(0.0, 100));
        System.out.println("scalb(NaN, 5)     = " + Math.scalb(Double.NaN, 5));
        double x = 0.1;
        System.out.println("round trip exact? " + (Math.scalb(Math.scalb(x, 50), -50) == x));
    }
}

How It Works

  1. Past the top exponent. The largest double is just under 21024, so 21024 itself overflows to Infinity.
  2. 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.
  3. 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.

Next: signum()

The sign of a number as -1.0, 0.0, or 1.0.

signum() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful