C++ 参考手册

位置:首页 > C++ 参考手册 >数值库 >常用数学函数 > std::cbrt, std::cbrtf, std::cbrtl

定义于头文件 <cmath>
float       cbrt ( float arg );
float       cbrtf( float arg );
(1) (C++11 起)
double      cbrt ( double arg );
(2) (C++11 起)
long double cbrt ( long double arg );
long double cbrtl( long double arg );
(3) (C++11 起)
double      cbrt ( IntegralType arg );
(4) (C++11 起)
1-3) 计算 arg 的立方根。
4) 接受任何整数类型参数的重载集或函数模板。等价于 2) (将参数转型为 double )。

参数

arg - 浮点或整数类型

返回值

若不出现错误,则返回 arg 的立方根( 3arg )。

若出现下溢所致的错误,则返回(舍入后的)正确结果。

错误处理

报告 math_errhandling 中指定的错误。

若实现支持 IEEE 浮点算术( IEC 60559 ),则

  • 若参数为 ±0 或 ±∞ ,则返回不更改的参数
  • 若参数为 NaN ,则返回 NaN 。

注解

std::cbrt(arg) 不等价于 std::pow(arg, 1.0/3) ,因为有理数
1
3
通常不等于 1.0/3 并且 std::pow 不能求负底数的小数次幂。另外 std::cbrt(arg) 常给出比 std::pow(arg, 1.0/3) 更精确的结果(见示例)。

示例

#include <iostream>
#include <cmath>
 
int main()
{
    // 正常使用
    std::cout << "cbrt(729) = " << std::cbrt(729) << '\n'
              << "cbrt(-0.125) = " << std::cbrt(-0.125) << '\n';
    // 特殊值
    std::cout << "cbrt(-0) = " << std::cbrt(-0.0) << '\n'
              << "cbrt(+inf) = " << std::cbrt(INFINITY) << '\n';
    // 精度
    std::cout.precision(std::numeric_limits<double>::max_digits10);
    std::cout << "cbrt(343)      = " << std::cbrt(343) << '\n';
    std::cout << "pow(343,1.0/3) = " << std::pow(343, 1.0/3) << '\n';
}

输出:

cbrt(729) = 9
cbrt(-0.125) = -0.5
cbrt(-0) = -0
cbrt(+inf) = inf
cbrt(343)      = 7
pow(343,1.0/3) = 6.9999999999999991

参阅

(C++11)(C++11)
求某数的给定次幂( xy
(函数)
(C++11)(C++11)
计算平方根( x
(函数)
(C++11)(C++11)(C++11)
计算两个给定数的平方和的平方根( x2
+y2

(函数)