exp2, exp2f, exp2l

< c‎ | numeric‎ | math
在标头 <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 <errno.h>
#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdio.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

引用

  • C23 标准(ISO/IEC 9899:2024):
  • 7.12.6.2 The exp2 functions (第 TBD 页)
  • 7.25 Type-generic math <tgmath.h> (第 TBD 页)
  • F.10.3.2 The exp2 functions (第 TBD 页)
  • C17 标准(ISO/IEC 9899:2018):
  • 7.12.6.2 The exp2 functions (第 177 页)
  • 7.25 Type-generic math <tgmath.h> (第 272-273 页)
  • F.10.3.2 The exp2 functions (第 379 页)
  • C11 标准(ISO/IEC 9899:2011):
  • 7.12.6.2 The exp2 functions (第 242-243 页)
  • 7.25 Type-generic math <tgmath.h> (第 373-375 页)
  • F.10.3.2 The exp2 functions (第 521 页)
  • C99 标准(ISO/IEC 9899:1999):
  • 7.12.6.2 The exp2 functions (第 223 页)
  • 7.22 Type-generic math <tgmath.h> (第 335-337 页)
  • F.9.3.2 The exp2 functions (第 458 页)

参阅

(C99)(C99)
计算 e 的给定次幂(ex
(函数)
(C99)(C99)(C99)
计算 e 的给定次幂减一(ex-1
(函数)
(C99)(C99)(C99)
计算底为 2 的对数(log2(x)
(函数)