Skip to content

Commit 95ccd9d

Browse files
authored
[Term Entry] C# Math-functions: Cbrt()
* Add documentation for the Math.Cbrt() method, including syntax, examples, and usage * minor content fixes * Minor changes ---------
1 parent dff1797 commit 95ccd9d

File tree

1 file changed

+85
-0
lines changed
  • content/c-sharp/concepts/math-functions/terms/cbrt

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
Title: 'Cbrt()'
3+
Description: 'Returns the cube root of the given number.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Arithmetic'
9+
- 'Methods'
10+
- 'Numbers'
11+
CatalogContent:
12+
- 'learn-c-sharp'
13+
- 'paths/computer-science'
14+
---
15+
16+
The **`Math.Cbrt()`** [method](https://www.codecademy.com/resources/docs/c-sharp/methods) in C# returns the cube root of a given number. It handles positive, negative, and special floating-point values such as `NaN` and infinities.
17+
18+
## Syntax
19+
20+
```pseudo
21+
Math.Cbrt(double x)
22+
```
23+
24+
**Parameters:**
25+
26+
- `x` (double): The number whose cube root is to be calculated.
27+
28+
**Return value:**
29+
30+
The method will return a value of [type](https://www.codecademy.com/resources/docs/c-sharp/data-types) `double` unless the value passed is one of the following:
31+
32+
- If `x` is `NaN`, the method will return `NaN`.
33+
- If `x` is `PositiveInfinity`, the method will return `PositiveInfinity`.
34+
- If `x` is `NegativeInfinity`, the method will return `NegativeInfinity`.
35+
- If `x` is negative, the method will return the real cube root (a negative number).
36+
37+
## Example
38+
39+
In this example, different numeric values are passed to `Math.Cbrt()` to calculate their cube roots:
40+
41+
```cs
42+
using System;
43+
44+
namespace MyCubeRoot {
45+
public class Example {
46+
public static void Main(string[] args) {
47+
double a = Math.Cbrt(27);
48+
double b = Math.Cbrt(-8);
49+
double c = Math.Cbrt(1000);
50+
double d = Math.Cbrt(0.125);
51+
52+
Console.WriteLine(a);
53+
Console.WriteLine(b);
54+
Console.WriteLine(c);
55+
Console.WriteLine(d);
56+
}
57+
}
58+
}
59+
```
60+
61+
This example results in the following output:
62+
63+
```shell
64+
3.0000000000000004
65+
-2
66+
10
67+
0.49999999999999994
68+
```
69+
70+
## Codebyte Example
71+
72+
In this example, the cube root of 64 is calculated using `Math.Cbrt()` and printed to the console:
73+
74+
```codebyte/csharp
75+
using System;
76+
77+
public class Example {
78+
public static void Main() {
79+
double number = 64;
80+
double cubeRoot = Math.Cbrt(number);
81+
82+
Console.WriteLine("The cube root of " + number + " is " + cubeRoot);
83+
}
84+
}
85+
```

0 commit comments

Comments
 (0)