Skip to content

Core\Math::cbrt

Returns the cube root of the number you give it. The cube root of 27.0 is 3.0, because 3 × 3 × 3 is 27.

A negative number works too. The cube root of -8.0 is -2.0, so the result always has the same sign as the number.

In plain words: a cube has the same length on every side. When you know how much space a cube fills, the cube root tells you how long each side is.

The examples below show a few cube roots, then negative numbers, then how a shop finds the size of a cube-shaped box that holds a given volume.

Parameters

  • $nfloatAny number.

Return value

Returns float. The cube root, with the same sign as $n: Core\Math::cbrt(-8.0) is -2.0. An infinity gives the same infinity, and NaN gives NaN.

Examples

A few cube roots

<?nvs
// Shows a few cube roots that `Core\Math::cbrt` returns.
// 3 × 3 × 3 is 27. This prints 3.
echo Core\Math::cbrt(27.0), "\n";
// 10 × 10 × 10 is 1000. This prints 10.
echo Core\Math::cbrt(1000.0), "\n";
// Most cube roots are not whole numbers. This prints 1.2599210498949.
echo Core\Math::cbrt(2.0), "\n";
Output
3
10
1.2599210498949

Negative numbers

<?nvs
// Shows that `Core\Math::cbrt` works for negative numbers.
// -2 × -2 × -2 is -8. This prints -2.
echo Core\Math::cbrt(-8.0), "\n";
// The result has the same sign as the number. This prints -0.1.
echo Core\Math::cbrt(-0.001), "\n";
// The `**` operator with a power of one third gives `NaN` for a negative number.
// `Core\Math::isNan` tests for it. This prints "not a number".
float $power = (-8.0) ** (1.0 / 3.0);
echo Core\Math::isNan($power) ? "not a number\n" : "a number\n";
Output
-2
-0.1
not a number

The size of a box

<?nvs
// Shows how a shop finds the size of a cube-shaped box that holds a given volume.
class Box
{
// Returns the length of each side, in centimetres, with one decimal place.
// One litre is 1000 cubic centimetres.
public static function side(float $litres): float
{
return Core\Math::round(Core\Math::cbrt($litres * 1000.0), { precision: 1 });
}
}
// A box for 64 litres. This prints 40.
echo Box::side(64.0), "\n";
// A box for 50 litres. This prints 36.8.
echo Box::side(50.0), "\n";
// A box for twice as much is only about a quarter bigger on each side. This prints 46.4.
echo Box::side(100.0), "\n";
Output
40
36.8
46.4