pow, powf, powl
来自cppreference.com
| 在标头 <math.h> 定义
|
||
| |
(1) | (C99 起) |
| |
(2) | |
| |
(3) | (C99 起) |
| 在标头 <tgmath.h> 定义
|
||
| |
(4) | (C99 起) |
1-3) 计算
base 的 exponent 次幂。4) 泛型宏:若任何实参拥有
long double 类型,则调用 powl。否则,若任何实参拥有整数类型或 double 类型,则调用 pow。否则调用 powf。若至少一个实参为复数或虚数,则宏调用对应的复函数(cpowf、cpow、cpowl)。参数
| base | - | 作为底的浮点数 |
| exponent | - | 作为指数的浮点数 |
返回值
若不出现错误,则返回 base 的 exponent 次幂(baseexponent
)。
若出现定义域错误,则返回实现定义值(支持的平台上为 NaN)。
若出现极点错误或上溢所致的值域错误,则返回 ±HUGE_VAL、±HUGE_VALF 或 ±HUGE_VALL。
若出现下溢所致的值域错误,则返回(舍入后的)正确结果。
错误处理
报告 math_errhandling 中指定的错误。
若 base 有限且为负,且 exponent 有限且为非整数,则出现定义域错误,并可能出现值域错误。
若 base 为零且 exponent 为零,则可能出现定义域错误。
若 base 为零且 exponent 为负,则可能出现定义域错误或极点错误。
若实现支持 IEEE 浮点数算术(IEC 60559),则
pow(+0, exponent),其中exponent为负奇数,返回+∞并引发 FE_DIVBYZEROpow(-0, exponent),其中exponent为负奇数,返回-∞并引发 FE_DIVBYZEROpow(±0, exponent),其中exponent为有限负数,且为偶数或非整数,则返回 +∞ 并引发 FE_DIVBYZEROpow(±0, -∞)返回 +∞ 并可能引发 FE_DIVBYZERO(C23 前)pow(+0, exponent),其中exponent为正奇数,返回 +0pow(-0, exponent),其中exponent为正奇数,返回 -0pow(±0, exponent),其中exponent为正非整数或正偶数,返回 +0pow(-1, ±∞)返回1pow(+1, exponent)对于任何exponent返回1,即使exponent为NaNpow(base, ±0)对于任何base返回1,即使base为NaNpow(base, exponent)返回NaN并引发 FE_INVALID,若base为有限负数且exponent为有限非整数。pow(base, -∞)对任何|base|<1返回 +∞pow(base, -∞)对任何|base|>1返回 +0pow(base, +∞)对任何|base|<1返回 +0pow(base, +∞)对任何|base|>1返回 +∞pow(-∞, exponent)返回 -0,若exponent为负奇整数pow(-∞, exponent)返回 +0,若exponent为负非整数或负偶数pow(-∞, exponent)返回 -∞,若exponent为正奇整数pow(-∞, exponent)返回 +∞,若exponent为正非整数或正偶数pow(+∞, exponent)对任何exponent返回 +0pow(+∞, exponent)对任何exponent返回 +∞- 除了指定于上处,若任何实参为 NaN,则返回 NaN
注解
尽管 pow 不能获得负数的开方根,也为 exponent 为 1 / 3 的常用情况提供了 cbrt 。
示例
运行此代码
#include <errno.h>
#include <fenv.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
int main(void)
{
// 典型使用
printf("pow(2, 10) = %f\n", pow(2,10));
printf("pow(2, 0.5) = %f\n", pow(2,0.5));
printf("pow(-2, -3) = %f\n", pow(-2,-3));
// 特殊值
printf("pow(-1, NAN) = %f\n", pow(-1,NAN));
printf("pow(+1, NAN) = %f\n", pow(+1,NAN));
printf("pow(INFINITY, 2) = %f\n", pow(INFINITY, 2));
printf("pow(INFINITY, -1) = %f\n", pow(INFINITY, -1));
// 错误处理
errno = 0; feclearexcept(FE_ALL_EXCEPT);
printf("pow(-1, 1/3) = %f\n", pow(-1, 1.0 / 3));
if (errno == EDOM)
perror(" errno == EDOM");
if (fetestexcept(FE_INVALID))
puts(" FE_INVALID raised");
feclearexcept(FE_ALL_EXCEPT);
printf("pow(-0, -3) = %f\n", pow(-0.0, -3));
if (fetestexcept(FE_DIVBYZERO))
puts(" FE_DIVBYZERO raised");
}
可能的输出:
pow(2, 10) = 1024.000000
pow(2, 0.5) = 1.414214
pow(-2, -3) = -0.125000
pow(-1, NAN) = nan
pow(+1, NAN) = 1.000000
pow(INFINITY, 2) = inf
pow(INFINITY, -1) = 0.000000
pow(-1, 1/3) = -nan
errno == EDOM: Numerical argument out of domain
FE_INVALID raised
pow(-0, -3) = -inf
FE_DIVBYZERO raised
引用
- C23 标准(ISO/IEC 9899:2024):
- 7.12.7.5 The pow functions
- 7.27 Type-generic math <tgmath.h>
- F.10.4.5 The pow functions (第 524-525 页)
- C17 标准(ISO/IEC 9899:2018):
- 7.12.7.4 The pow functions (第 248-249 页)
- 7.25 Type-generic math <tgmath.h> (第 373-375 页)
- F.10.4.4 The pow functions (第 524-525 页)
- C11 标准(ISO/IEC 9899:2011):
- 7.12.7.4 The pow functions (第 248-249 页)
- 7.25 Type-generic math <tgmath.h> (第 373-375 页)
- F.10.4.4 The pow functions (第 524-525 页)
- C99 标准(ISO/IEC 9899:1999):
- 7.12.7.4 The pow functions (第 229 页)
- 7.22 Type-generic math <tgmath.h> (第 335-337 页)
- F.9.4.4 The pow functions (第 461 页)
- C89/C90 标准(ISO/IEC 9899:1990):
- 4.5.5.1 The pow function
参阅
(C99)(C99) |
计算平方根(√x) (函数) |
(C99)(C99)(C99) |
计算立方根(3√x) (函数) |
(C99)(C99)(C99) |
计算两个给定数平方和的平方根(√x2 +y2 ) (函数) |
(C99)(C99)(C99) |
计算复数幂函数 (函数) |
pow 的 C++ 文档
| |