std::cbrt, std::cbrtf, std::cbrtl
来自 cppreference.cn
定义于头文件 <cmath> |
||
(1) | ||
float cbrt ( float num ); double cbrt ( double num ); |
(直到 C++23) | |
/*floating-point-type*/ cbrt ( /*floating-point-type*/ num ); |
(自 C++23 起) (constexpr 自 C++26 起) |
|
float cbrtf( float num ); |
(2) | (自 C++11 起) (constexpr 自 C++26 起) |
long double cbrtl( long double num ); |
(3) | (自 C++11 起) (constexpr 自 C++26 起) |
SIMD 重载 (自 C++26 起) |
||
定义于头文件 <simd> |
||
template< /*math-floating-point*/ V > constexpr /*deduced-simd-t*/<V> |
(S) | (自 C++26 起) |
附加重载 (自 C++11 起) |
||
定义于头文件 <cmath> |
||
template< class Integer > double cbrt ( Integer num ); |
(A) | (constexpr 自 C++26 起) |
1-3) 计算 num 的立方根。 库为所有 cv 非限定浮点类型提供了
std::cbrt
的重载作为参数类型。(自 C++23 起)
S) SIMD 重载对 v_num 执行元素级
std::cbrt 。
|
(自 C++26 起) |
A) 为所有整数类型提供了附加重载,它们被视为 double。
|
(自 C++11 起) |
目录 |
[编辑] 参数
num | - | 浮点值或整数值 |
[编辑] 返回值
如果没有错误发生,则返回 num 的立方根 (3√num)。
如果由于下溢发生范围错误,则返回正确的结果(舍入后)。
[编辑] 错误处理
错误报告按照 math_errhandling 中的规定。
如果实现支持 IEEE 浮点算术 (IEC 60559),
- 如果参数是 ±0 或 ±∞,则原样返回。
- 如果参数是 NaN,则返回 NaN。
[编辑] 注解
std::cbrt(num) 不等同于 std::pow(num, 1.0 / 3),因为有理数1 |
3 |
附加重载不一定需要完全按照 (A) 的方式提供。它们只需要足以确保对于整数类型的参数 num,std::cbrt(num) 具有与 std::cbrt(static_cast<double>(num)) 相同的效果。
[编辑] 示例
运行此代码
#include <cmath> #include <iomanip> #include <iostream> #include <limits> int main() { std::cout << "Normal use:\n" << "cbrt(729) = " << std::cbrt(729) << '\n' << "cbrt(-0.125) = " << std::cbrt(-0.125) << '\n' << "Special values:\n" << "cbrt(-0) = " << std::cbrt(-0.0) << '\n' << "cbrt(+inf) = " << std::cbrt(INFINITY) << '\n' << "Accuracy and comparison with `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'; }
可能的输出
Normal use: cbrt(729) = 9 cbrt(-0.125) = -0.5 Special values: cbrt(-0) = -0 cbrt(+inf) = inf Accuracy and comparison with `pow`: cbrt(343) = 7 pow(343,1.0/3) = 6.9999999999999991 cbrt(-343) = -7 pow(-343,1.0/3) = -nan
[编辑] 参见
(C++11)(C++11) |
将数字提升到给定幂 (xy) (函数) |
(C++11)(C++11) |
计算平方根 (√x) (函数) |
(C++11)(C++11)(C++11) |
计算斜边 √x2 +y2 和 √x2 +y2 +z2 (自 C++17 起) (函数) |
C 文档 关于 cbrt
|