Namespaces
Variants

std:: cbrt, std:: cbrtf, std:: cbrtl

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
定义于头文件 <cmath>
(1)
float cbrt ( float num ) ;

double cbrt ( double num ) ;

long double cbrt ( long double num ) ;
(C++23 前)
/*floating-point-type*/
cbrt ( /*floating-point-type*/ num ) ;
(C++23 起)
(C++26 起为 constexpr)
float cbrtf ( float num ) ;
(2) (C++11 起)
(C++26 起为 constexpr)
long double cbrtl ( long double num ) ;
(3) (C++11 起)
(C++26 起为 constexpr)
SIMD 重载 (C++26 起)
定义于头文件 <simd>
template < /*math-floating-point*/ V >

constexpr /*deduced-simd-t*/ < V >

cbrt ( const V & v_num ) ;
(S) (C++26 起)
附加重载 (C++11 起)
定义于头文件 <cmath>
template < class Integer >
double cbrt ( Integer num ) ;
(A) (C++26 起为 constexpr)
1-3) 计算 num 的立方根。 标准库为所有 cv 未限定浮点类型提供了 std::cbrt 的重载版本作为参数类型。 (C++23 起)
S) SIMD重载对 v_num 执行逐元素的 std::cbrt 运算。
(参见 math-floating-point deduced-simd-t 了解其定义。)
(since C++26)
A) 为所有整数类型提供了额外的重载,这些类型被视为 double
(since C++11)

目录

参数

num - 浮点数或整数值

返回值

如果未发生错误,则返回 num 的立方根 ( 3 num )。

如果由于下溢发生范围错误,将返回正确结果(舍入后)。

错误处理

错误报告方式遵循 math_errhandling 中的规范。

如果实现支持 IEEE 浮点算术 (IEC 60559),

  • 若参数为 ±0 或 ±∞,则保持不变直接返回。
  • 若参数为 NaN,则返回 NaN。

注释

std :: cbrt ( num ) is not equivalent to std:: pow ( num, 1.0 / 3 ) because the rational number
1
3
is typically not equal to 1.0 / 3 and std::pow cannot raise a negative base to a fractional exponent. Moreover, std :: cbrt ( num ) usually gives more accurate results than std:: pow ( num, 1.0 / 3 ) (see example).

额外的重载不需要完全按照 (A) 提供。只需确保对于整数类型的实参 num std :: cbrt ( num ) std :: cbrt ( static_cast < double > ( num ) ) 具有相同效果即可。

示例

#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
int main()
{
    std::cout
        << "常规用法:\n"
        << "cbrt(729)       = " << std::cbrt(729) << '\n'
        << "cbrt(-0.125)    = " << std::cbrt(-0.125) << '\n'
        << "特殊值:\n"
        << "cbrt(-0)        = " << std::cbrt(-0.0) << '\n'
        << "cbrt(+inf)      = " << std::cbrt(INFINITY) << '\n'
        << "精度及与 `pow` 的比较:\n"
        << std::setprecision(std::numeric_limits<double>::max_digits10)
        << "cbrt(343)       = " << std::cbrt(343) << '\n'
        << "pow(343,1.0/3)  = " << std::pow(343, 1.0 / 3) << '\n'
        << "cbrt(-343)      = " << std::cbrt(-343) << '\n'
        << "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
精度及与 `pow` 的比较:
cbrt(343)       = 7
pow(343,1.0/3)  = 6.9999999999999991
cbrt(-343)      = -7
pow(-343,1.0/3) = -nan

参见

(C++11) (C++11)
计算数的指定次幂 ( x y )
(函数)
(C++11) (C++11)
计算平方根 ( x )
(函数)
(C++11) (C++11) (C++11)
计算斜边长 x 2
+y 2
x 2
+y 2
+z 2
(C++17 起)

(函数)
C 文档 for cbrt