In C#, Math.ScaleB(x, n) returns x * 2n: your number multiplied by two, n times (or divided by two, n times, when n is negative). Math.ScaleB(3, 4) is 3 x 16 = 48.
Think of a "zoom" knob that only doubles or halves. A double is stored as a small number times a power of two, and ScaleB simply changes that power. This is exact: no rounding error is added, unlike x * Math.Pow(2, n) for extreme values. It is an advanced tool, but the idea is easy: each step up in n doubles the answer, each step down halves it. Its partner is ILogB(), which reads the power back out.
This is a static member of the static class System.Math. You call it with the class name, such as Math.ScaleB(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Math.ScaleB was added in .NET Core 3.0. It is not in classic .NET Framework.
Syntax
Pass the number and the int power of two. The answer is a double.
C#
double a = Math.ScaleB(3, 4); // 3 * 16 = 48
double b = Math.ScaleB(1, 10); // 1024
double c = Math.ScaleB(1, -3); // 0.125 (1 / 8)
Call
What it does
Math.ScaleB(3, 4)
3 x 2 four times over: 48.
Math.ScaleB(10, 0)
Zero doublings: 10, unchanged.
Math.ScaleB(1, -3)
Three halvings: 0.125.
Math.ScaleB(1, 1024)
Too big for a double: Infinity.
n is an int that can be negative. If the result is too large, you get Infinity. If it is too small, you get 0 (or a tiny "subnormal" number that has lost some precision). A NaN or zero input stays the same. The method does not throw.
Library declarations
C#
public static double ScaleB(double x, int n);
Parameters
Parameter
Description
x
Required. The number to scale.
n
Required. An int power of two. Positive doubles the number n times, negative halves it.
Return Value
Return Value
Description
double
x * 2n. Infinity on overflow, 0 on underflow, and NaN if x is NaN.
C# Examples
Example 1 — Multiply by Powers of Two
Scale a few numbers up and keep one at zero steps.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(Math.ScaleB(3, 4));
Console.WriteLine(Math.ScaleB(1, 10));
Console.WriteLine(Math.ScaleB(10, 0));
Console.WriteLine(Math.ScaleB(7, 1));
Console.WriteLine(Math.ScaleB(-3, 2));
}
}
Output
48
1024
10
14
-12
How It Works
Four doublings. 2 to the power 4 is 16, so ScaleB(3, 4) is 3 x 16 = 48.
Powers of two. Starting from 1, ten doublings give 1024. This is the same as 1 shifted left ten times.
Zero steps.20 is 1, so the number does not change.
One step, and a negative number.ScaleB(7, 1) doubles once, to 14. The sign is kept: -3 x 4 is -12.
Example 2 — Negative n Divides by Two
Use a negative power to halve a number, and compare with Math.Pow.
C#
using System;
using System.Globalization;
class Program {
static void Main() {
Console.WriteLine(Math.ScaleB(1, -1).ToString(CultureInfo.InvariantCulture));
Console.WriteLine(Math.ScaleB(1, -3).ToString(CultureInfo.InvariantCulture));
Console.WriteLine(Math.ScaleB(3, -2).ToString(CultureInfo.InvariantCulture));
double viaPow = 3 * Math.Pow(2, 4);
Console.WriteLine(viaPow == Math.ScaleB(3, 4));
}
}
Output
0.5
0.125
0.75
True
How It Works
One halving.ScaleB(1, -1) is 1 divided by 2, which is 0.5.
Three halvings. 1 divided by 2 three times is 1/8, or 0.125. Powers of two are exact in a double.
Any starting number.ScaleB(3, -2) is 3 divided by 4, which is 0.75.
Same as Pow here. For ordinary values, 3 * Math.Pow(2, 4) gives the same 48 as ScaleB, so the comparison is True. ScaleB is simply more direct and exact.
Example 3 — Split and Rebuild, Overflow and Underflow
Pair ScaleB with ILogB to split a number, then see what happens at the edges.
C#
using System;
using System.Globalization;
class Program {
static void Main() {
double x = 48;
int power = Math.ILogB(x);
double mantissa = Math.ScaleB(x, -power);
Console.WriteLine(power);
Console.WriteLine(mantissa.ToString(CultureInfo.InvariantCulture));
Console.WriteLine(Math.ScaleB(mantissa, power));
Console.WriteLine(Math.ScaleB(1, 1024));
Console.WriteLine(Math.ScaleB(1, -2000));
Console.WriteLine(Math.ScaleB(double.NaN, 3));
}
}
Output
5
1.5
48
Infinity
0
NaN
How It Works
Find the power.Math.ILogB(48) is 5, because 48 sits between 25 (32) and 26 (64).
Split.ScaleB(48, -5) divides by 32 and leaves 1.5. Every number can be written as a value between 1 and 2 times a power of two.
Rebuild.ScaleB(1.5, 5) multiplies by 32 again and gives exactly 48. No rounding error appears.
Edges. 2 to the power 1024 is too big, so the answer is Infinity. 2 to the power -2000 is too small, so you get 0. A NaN input stays NaN.
Frequently Asked Questions
It returns x multiplied by 2 to the power n. Math.ScaleB(3, 4) is 48 and Math.ScaleB(1, -3) is 0.125.
It was added in .NET Core 3.0, so it is in modern .NET. In classic .NET Framework use x * Math.Pow(2, n).
ScaleB changes the binary exponent directly, so it adds no rounding error and does not overflow in the middle (for example Pow(2, 1100) alone would already be Infinity). For everyday numbers both give the same answer.
Yes. A negative n divides by a power of two. Math.ScaleB(1, -1) is 0.5.
No. Math is a static class, so you call Math.ScaleB(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.