在 C 中求立方根
Find cube root in C
我正在尝试查找体积的立方根,但效果不佳。我认为浮点数有问题,但不知道如何解决。如您所见,立方根应该是 3 而不是 37
#include <stdio.h>
int main() {
int width;
int length;
int height;
int volume;
double volumes;
printf("write width ");
scanf("%d", &width);
printf("write length ");
scanf("%d", &length);
printf("write height ");
scanf("%d", &height);
volume = width * length * height;
printf("volume is %d \n", volume);
volumes = (double)pow((double)volume,1.0/ 3.0);
printf("cube root is %f \n", volumes);
return 0;
}
您可以在 math.h 文件中使用 cbrt() 函数
#include<math.h>
//cbrt(x) to get cube root of x :)
printf("cube root is %f \n",cbrt(x));
我正在尝试查找体积的立方根,但效果不佳。我认为浮点数有问题,但不知道如何解决。如您所见,立方根应该是 3 而不是 37
#include <stdio.h>
int main() {
int width;
int length;
int height;
int volume;
double volumes;
printf("write width ");
scanf("%d", &width);
printf("write length ");
scanf("%d", &length);
printf("write height ");
scanf("%d", &height);
volume = width * length * height;
printf("volume is %d \n", volume);
volumes = (double)pow((double)volume,1.0/ 3.0);
printf("cube root is %f \n", volumes);
return 0;
}
您可以在 math.h 文件中使用 cbrt() 函数
#include<math.h>
//cbrt(x) to get cube root of x :)
printf("cube root is %f \n",cbrt(x));