cbrt() Function
The cbrt()
function in C computes the cubic root of a given value and returns the result. It handles both positive and negative inputs correctly, providing a real-number output even when the input is negative. The function is available in different variants to support double, float, and long double data types.
Syntax of cbrt()
double cbrt(double x);
Parameters
Parameter | Description |
---|---|
x | The value whose cubic root is computed. |
Return Value
The function returns the cubic root of the provided value.
The function computes the real cube root of the input value. For negative inputs, the returned value is also negative.
Examples for cbrt()
Example 1: Computing the Cubic Root of a Positive Number
This example demonstrates how to compute the cubic root of a positive number using cbrt()
.
Program
#include <stdio.h>
#include <math.h>
int main() {
double num = 27.0;
double result = cbrt(num);
printf("The cubic root of %.2f is %.2f\n", num, result);
return 0;
}
Explanation:
- The variable
num
is initialized with the positive value27.0
. - The
cbrt()
function computes the cubic root of the given value. - The resulting value, which is
3.00
, is printed as the cubic root of 27.
Program Output:
The cubic root of 27.00 is 3.00
Example 2: Computing the Cubic Root of a Negative Number
This example demonstrates how cbrt()
handles negative inputs by computing the negative cubic root.
Program
#include <stdio.h>
#include <math.h>
int main() {
double num = -8.0;
double result = cbrt(num);
printf("The cubic root of %.2f is %.2f\n", num, result);
return 0;
}
Explanation:
- The variable
num
is assigned a negative value of-8.0
. - The function
cbrt()
calculates the cubic root, which is negative for negative inputs. - The resulting value,
-2.00
, is printed as the cubic root of -8.
Program Output:
The cubic root of -8.00 is -2.00
Example 3: Using cbrtf() for Float Precision Calculations
This example illustrates the use of cbrtf()
to compute the cubic root of a floating-point value.
Program
#include <stdio.h>
#include <math.h>
int main() {
float num = 64.0f;
float result = cbrtf(num);
printf("The cubic root of %.2f is %.2f\n", num, result);
return 0;
}
Explanation:
- The variable
num
is defined as a float with the value64.0f
. - The function
cbrtf()
computes the cubic root of the float value. - The resulting value,
4.00
, is printed as the cubic root of 64.
Program Output:
The cubic root of 64.00 is 4.00