Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
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.
