跳到主要内容

Cbrt

定义于头文件 <cmath> 中。

描述

计算 num 的立方根。该库为所有 cv-unqualified 浮点类型提供了 std::cbrt 的重载,作为参数 num 的类型。附加重载为所有整数类型提供,它们被视为 double

声明

// 1)
/* floating-point-type */ cbrt( /* floating-point-type */ num );
// 2)
float cbrtf( float num );
// 3)
long double cbrtl( long double num );
附加重载
// 4)
template< class Integer >
double cbrt ( Integer num );

参数

num - 浮点或整数值

返回值

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

如果因下溢导致范围错误,则返回正确结果(舍入后)。

错误处理

错误按 math_errhandling 中指定的方式报告。

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

如果参数是 ±0±∞,则返回原值;如果参数是 NaN,则返回 NaN

备注

std::cbrt(num) 不等同于 std::pow(num, 1.0 / 3),因为有理数 通常不等于 1.0 / 3,并且 std::pow 不能将负底数提升为分数指数。此外,std::cbrt(num) 通常比 std::pow(num, 1.0 / 3) 给出更准确的结果 (参见示例)
附加重载无需完全按照附加重载提供。它们只需要足以确保对于其整数类型的参数numstd::cbrt(num)std::cbrt(static_cast<double>(num))具有相同的效果。

示例

#include <cmath>
#include <limits>
#include <iomanip>
#include <iostream>

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

Cbrt

定义于头文件 <cmath> 中。

描述

计算 num 的立方根。该库为所有 cv-unqualified 浮点类型提供了 std::cbrt 的重载,作为参数 num 的类型。附加重载为所有整数类型提供,它们被视为 double

声明

// 1)
/* floating-point-type */ cbrt( /* floating-point-type */ num );
// 2)
float cbrtf( float num );
// 3)
long double cbrtl( long double num );
附加重载
// 4)
template< class Integer >
double cbrt ( Integer num );

参数

num - 浮点或整数值

返回值

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

如果因下溢导致范围错误,则返回正确结果(舍入后)。

错误处理

错误按 math_errhandling 中指定的方式报告。

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

如果参数是 ±0±∞,则返回原值;如果参数是 NaN,则返回 NaN

备注

std::cbrt(num) 不等同于 std::pow(num, 1.0 / 3),因为有理数 通常不等于 1.0 / 3,并且 std::pow 不能将负底数提升为分数指数。此外,std::cbrt(num) 通常比 std::pow(num, 1.0 / 3) 给出更准确的结果 (参见示例)
附加重载无需完全按照附加重载提供。它们只需要足以确保对于其整数类型的参数numstd::cbrt(num)std::cbrt(static_cast<double>(num))具有相同的效果。

示例

#include <cmath>
#include <limits>
#include <iomanip>
#include <iostream>

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