In C#, Math.Cbrt(d) returns the cube root of a number: the value that gives d when you multiply it by itself three times. Math.Cbrt(27) is about 3, because 3 x 3 x 3 = 27.
Think of a cube-shaped box with a volume of 125 litres. Each edge is Math.Cbrt(125) = 5 units long. Unlike the square root (Sqrt()), the cube root works for negative numbers: Math.Cbrt(-27) is -3. This is the reason Cbrt exists: Math.Pow(-27, 1.0 / 3) returns NaN.
This is a static member of the static class System.Math. You call it with the class name, such as Math.Cbrt(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Math.Cbrt was added in .NET Core 2.1 and .NET Standard 2.1. It is not in classic .NET Framework.
Syntax
Pass one number. The answer is a double.
C#
double a = Math.Cbrt(27); // 3
double b = Math.Cbrt(-8); // -2
double c = Math.Cbrt(1000); // 10
Call
What it does
Math.Cbrt(27)
The cube root, 3.
Math.Cbrt(-27)
-3. The sign is kept, unlike a square root.
Math.Cbrt(0)
Returns 0.
Math.Cbrt(double.NaN)
Returns NaN.
The result is a double and may differ from the exact answer in the very last digits, so the examples below round it with Math.Round(value, 6). There is no decimal overload. Compare with a tolerance rather than ==.
Library declarations
C#
public static double Cbrt(double d);
Parameters
Parameter
Description
d
Required. The number to take the cube root of. It may be negative, zero, or positive.
Return Value
Return Value
Description
double
The cube root of d, with the same sign as d. NaN if d is NaN. Infinity gives infinity.
C# Examples
Example 1 — Cube Roots of Perfect Cubes
Take the cube root of numbers whose roots are whole, rounding to remove tiny floating-point noise.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(Math.Round(Math.Cbrt(27), 6));
Console.WriteLine(Math.Round(Math.Cbrt(8), 6));
Console.WriteLine(Math.Round(Math.Cbrt(1000), 6));
Console.WriteLine(Math.Round(Math.Cbrt(1), 6));
Console.WriteLine(Math.Round(Math.Cbrt(0), 6));
}
}
Output
3
2
10
1
0
How It Works
27 gives 3. The cube root asks "which number used three times makes 27?". 3 x 3 x 3 is 27.
8 and 1000. The cube roots are 2 and 10. Perfect cubes give whole-number roots.
Why Math.Round. A double answer may be off in the 15th decimal place on some systems. Rounding to 6 places gives a clean result everywhere.
1 and 0. The cube root of 1 is 1, and the cube root of 0 is 0.
Example 2 — Cbrt Handles Negatives
Compare Cbrt with Pow on a negative number, then find the side of a cube from its volume.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(Math.Round(Math.Cbrt(-27), 6));
Console.WriteLine(Math.Pow(-27, 1.0 / 3));
double volume = 125;
double side = Math.Cbrt(volume);
Console.WriteLine(Math.Round(side, 6));
Console.WriteLine(Math.Round(side * side * side, 6));
}
}
Output
-3
NaN
5
125
How It Works
Negative works. -3 x -3 x -3 is -27, so the cube root of -27 is -3. Cbrt keeps the sign.
Pow fails.Math.Pow(-27, 1.0 / 3) is a negative base with a fractional exponent, so it returns NaN.
Side of a cube. A cube with volume 125 has edge length Cbrt(125), which is 5.
Check the answer. Multiplying the side by itself three times gives the volume 125 again, after rounding away any noise.
Example 3 — Pow Can Be Slightly Off
Compare a cube root from Pow with one from Cbrt, and look at NaN and infinity.
Two routes.Pow(64, 1.0 / 3) uses the fraction 1/3, which cannot be stored exactly. Cbrt(64) is made for cube roots.
Same answer, rounded. Both round to 4. The Pow result is usually 3.9999999999999996 before rounding, so never compare it with == 4.
Tolerance test. The gap between the two answers is far smaller than 0.000001, so the comparison prints True.
Special values. The cube root of positive infinity is Infinity, and the cube root of NaN is NaN.
Frequently Asked Questions
It returns the cube root of a number as a double. Math.Cbrt(27) is 3 and Math.Cbrt(-8) is -2.
It was added in .NET Core 2.1 and .NET Standard 2.1, so it is in modern .NET. In classic .NET Framework, use Math.Sign(x) * Math.Pow(Math.Abs(x), 1.0 / 3).
Pow returns NaN for a negative x, and 1/3 is not stored exactly, so the answer can be a tiny bit off (for example 3.9999999999999996 instead of 4). Cbrt handles both cases.
Yes. The cube root of a negative number is negative, so Math.Cbrt(-27) is -3. That is the main difference from Math.Sqrt.
No. Math is a static class, so you call Math.Cbrt(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.