cbrt, cbrtf, cbrtl
来自cppreference.com
| 在标头 <math.h> 定义
|
||
| |
(1) | (C99 起) |
| |
(2) | (C99 起) |
| |
(3) | (C99 起) |
| 在标头 <tgmath.h> 定义
|
||
| |
(4) | (C99 起) |
1-3) 计算
arg 的立方根。4) 泛型宏:若
arg 拥有 long double 类型,则调用 cbrtl。否则,若 arg 拥有整数类型或 double 类型,则调用 cbrt。否则,调用 cbrtf。参数
| arg | - | 浮点数 |
返回值
若不出现错误,则返回 arg 的立方根(3√arg)。
若出现下溢所致的错误,则返回(舍入后的)正确结果。
错误处理
报告 math_errhandling 中指定的错误。
若实现支持 IEEE 浮点数算术( IEC 60559 ),则
- 若参数为 ±0 或 ±∞ ,则返回不更改的参数
- 若参数为 NaN ,则返回 NaN 。
注解
cbrt(arg) 不等价于 pow(arg, 1.0/3),因为有理数
| 1 |
| 3 |
通常不等于 1.0/3 并且 pow 不能求负底数的小数次幂。另外 cbrt(arg) 常给出比 pow(arg, 1.0/3) 更精确的结果(见示例)。
示例
运行此代码
#include <float.h>
#include <math.h>
#include <stdio.h>
int main(void)
{
printf("普通用法:\n"
"cbrt(729) = %f\n", cbrt(729));
printf("cbrt(-0.125) = %f\n", cbrt(-0.125));
printf("特殊值:\n"
"cbrt(-0) = %f\n", cbrt(-0.0));
printf("cbrt(+inf) = %f\n", cbrt(INFINITY));
printf("精度:\n"
"cbrt(343) = %.*f\n", DBL_DECIMAL_DIG, cbrt(343));
printf("pow(343,1.0/3) = %.*f\n", DBL_DECIMAL_DIG, pow(343, 1.0/3));
}
可能的输出:
普通用法:
cbrt(729) = 9.000000
cbrt(-0.125) = -0.500000
特殊值:
cbrt(-0) = -0.000000
cbrt(+inf) = inf
精度:
cbrt(343) = 7.00000000000000000
pow(343,1.0/3) = 6.99999999999999911
引用
- C23 标准(ISO/IEC 9899:2024):
- 7.12.7.1 The cbrt functions (第 TBD 页)
- 7.25 Type-generic math <tgmath.h> (第 TBD 页)
- F.10.4.1 The cbrt functions (第 TBD 页)
- C17 标准(ISO/IEC 9899:2018):
- 7.12.7.1 The cbrt functions (第 180-181 页)
- 7.25 Type-generic math <tgmath.h> (第 272-273 页)
- F.10.4.1 The cbrt functions (第 381- 页)
- C11 标准(ISO/IEC 9899:2011):
- 7.12.7.1 The cbrt functions (第 247 页)
- 7.25 Type-generic math <tgmath.h> (第 373-375 页)
- F.10.4.1 The cbrt functions (第 524 页)
- C99 标准(ISO/IEC 9899:1999):
- 7.12.7.1 The cbrt functions (第 228 页)
- 7.22 Type-generic math <tgmath.h> (第 335-337 页)
- F.9.4.1 The cbrt functions (第 460 页)
参阅
(C99)(C99) |
计算一个数的给定次幂(xy) (函数) |
(C99)(C99) |
计算平方根(√x) (函数) |
(C99)(C99)(C99) |
计算两个给定数平方和的平方根(√x2 +y2 ) (函数) |
cbrt 的 C++ 文档
| |