MathF.Cbrt() Method in C# with Examples

The MathF.Cbrt() method in C# is used to return the cube root of a floating-point value. This method is part of the System namespace and works specifically with float data types, providing better performance than the generic Math.Cbrt() method when working with single-precision floating-point numbers.

Syntax

Following is the syntax −

public static float Cbrt(float val);

Parameters

  • val − A floating-point number whose cube root is to be calculated.

Return Value

Returns a float value representing the cube root of the specified number. If the input is NaN (Not a Number), the method returns NaN. For negative numbers, it returns the negative cube root.

Cube Root Operation Input 64.0f (val) ? Cbrt() Output 4.0f (result) Because 4³ = 4 × 4 × 4 = 64

Using MathF.Cbrt() with Perfect Cubes

Example

using System;

class Demo {
   public static void Main() {
      float val1 = 64f;
      float val2 = 27f;
      float val3 = 125f;
      
      Console.WriteLine("Cube root of " + val1 + " = " + MathF.Cbrt(val1));
      Console.WriteLine("Cube root of " + val2 + " = " + MathF.Cbrt(val2));
      Console.WriteLine("Cube root of " + val3 + " = " + MathF.Cbrt(val3));
   }
}

The output of the above code is −

Cube root of 64 = 4
Cube root of 27 = 3
Cube root of 125 = 5

Using MathF.Cbrt() with Decimal Values

Example

using System;

class Demo {
   public static void Main() {
      float val1 = 12.67f;
      float val2 = 0.008f;
      float val3 = 1000.5f;
      
      Console.WriteLine("Cube root of " + val1 + " = " + MathF.Cbrt(val1));
      Console.WriteLine("Cube root of " + val2 + " = " + MathF.Cbrt(val2));
      Console.WriteLine("Cube root of " + val3 + " = " + MathF.Cbrt(val3));
   }
}

The output of the above code is −

Cube root of 12.67 = 2.331268
Cube root of 0.008 = 0.2
Cube root of 1000.5 = 10.001666

Using MathF.Cbrt() with Negative Numbers

Example

using System;

class Demo {
   public static void Main() {
      float val1 = -64f;
      float val2 = -27f;
      float val3 = -8f;
      
      Console.WriteLine("Cube root of " + val1 + " = " + MathF.Cbrt(val1));
      Console.WriteLine("Cube root of " + val2 + " = " + MathF.Cbrt(val2));
      Console.WriteLine("Cube root of " + val3 + " = " + MathF.Cbrt(val3));
   }
}

The output of the above code is −

Cube root of -64 = -4
Cube root of -27 = -3
Cube root of -8 = -2

Conclusion

The MathF.Cbrt() method provides an efficient way to calculate cube roots of floating-point numbers in C#. It handles both positive and negative values correctly, returning the real cube root for negative numbers, making it useful for mathematical calculations requiring cube root operations.

Updated on: 2026-03-17T07:04:36+05:30

161 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements