gpt4 book ai didi

C - 使用 pow 函数求负数的立方根

转载 作者:太空狗 更新时间:2023-10-29 16:40:05 31 4
gpt4 key购买 nike

在现实世界中负数的立方根应该存在:cuberoot(-1)=-1,即(-1)*(-1)*(-1)=-1或者cuberoot(-27)=-3,即(-3)*(-3)*(-3)=-27

但是当我在 C 中使用 pow 函数计算负数的立方根时,我得到 nan(不是数字)

double cuber;
cuber=pow((-27.),(1./3.));
printf("cuber=%f\n",cuber);

输出:cuber=nan

在C语言中有没有办法计算负数的立方根?

最佳答案

7.12.7.1 cbrt 函数

概要

#include <math.h>
double cbrt(double x);
float cbrtf(float x);
long double cbrtl(long double x);

描述

cbrt 函数计算x 的实立方根。


如果您好奇,pow 不能用于计算立方根,因为三分之一不能表示为 float 。您实际上是在要求 pow-27.0 提高到非常接近 1/3 的有理幂;没有合适的实际结果。

关于C - 使用 pow 函数求负数的立方根,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8493005/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com