exp2, exp2f, exp2l
来自cppreference.com
| 定义于头文件 <math.h>
|
||
| float exp2f( float n ); |
(1) | (C99 起) |
| double exp2( double n ); |
(2) | (C99 起) |
| long double exp2l( long double n ); |
(3) | (C99 起) |
| 定义于头文件 <tgmath.h>
|
||
| #define exp2( n ) |
(4) | (C99 起) |
1-3) 计算 2 的给定
n 次幂。4) 泛型宏,若
n 拥有 long double 类型,则调用 exp2l 。否则,若 n 用有整数类型或 double 类型,则调用 exp2 。否则调用 exp2f 。参数
| n | - | 浮点值 |
返回值
若不出现错误,则返回 n 的底 2 指数( 2n
)。
若出现上溢所致的值域错误,则返回 +HUGE_VAL 、 +HUGE_VALF 或 +HUGE_VALL 。
若出现下溢所致的值域错误,则返回(舍入后的)正确结果。
错误处理
报告 math_errhandling 中指定的错误。
若实现支持 IEEE 浮点算术( IEC 60559 ),则
- 若参数为 ±0 ,则返回 1
- 若参数为 -∞ ,则返回 +0
- 若参数为 +∞ ,则返回 +∞
- 若参数为 NaN ,则返回 NaN
示例
运行此代码
#include <stdio.h> #include <math.h> #include <float.h> #include <errno.h> #include <fenv.h> #pragma STDC FENV_ACCESS ON int main(void) { printf("exp2(5) = %f\n", exp2(5)); printf("exp2(0.5) = %f\n", exp2(0.5)); printf("exp2(-4) = %f\n", exp2(-4)); // 特殊值 printf("exp2(-0.9) = %f\n", exp2(-0.9)); printf("exp2(-Inf) = %f\n", exp2(-INFINITY)); // 错误处理 errno = 0; feclearexcept(FE_ALL_EXCEPT); printf("exp2(1024) = %f\n", exp2(1024)); if(errno == ERANGE) perror(" errno == ERANGE"); if(fetestexcept(FE_OVERFLOW)) puts(" FE_OVERFLOW raised"); }
可能的输出:
exp2(5) = 32.000000
exp2(0.5) = 1.414214
exp2(-4) = 0.062500
exp2(-0.9) = 0.535887
exp2(-Inf) = 0.000000
exp2(1024) = Inf
errno == ERANGE: Result too large
FE_OVERFLOW raised引用
- C11 standard (ISO/IEC 9899:2011):
- 7.12.6.2 The exp2 functions (p: 242-243)
- 7.25 Type-generic math <tgmath.h> (p: 373-375)
- F.10.3.2 The exp2 functions (p: 521)
- C99 standard (ISO/IEC 9899:1999):
- 7.12.6.2 The exp2 functions (p: 223)
- 7.22 Type-generic math <tgmath.h> (p: 335-337)
- F.9.3.2 The exp2 functions (p: 458)
参阅
| (C99)(C99) |
计算 e 的给定次幂 ( ex ) (函数) |
| (C99)(C99)(C99) |
计算 e 的给定次幂减一( ex-1 ) (函数) |
| (C99)(C99)(C99) |
计算底为 2 的对数( log2(x) ) (函数) |