Namespaces
Variants

exp2, exp2f, exp2l

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
(C23)
exp2
(C99)
(C99)
(C23)
(C23)

Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <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 为底的指数( 2 n
)。

如果发生因上溢导致的范围错误,则返回 +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: 结果过大
    FE_OVERFLOW raised

参考文献

  • C23 标准 (ISO/IEC 9899:2024):
  • 7.12.6.2 exp2 函数 (p: TBD)
  • 7.25 类型泛型数学 <tgmath.h> (p: TBD)
  • F.10.3.2 exp2 函数 (p: TBD)
  • C17 标准 (ISO/IEC 9899:2018):
  • 7.12.6.2 exp2 函数 (p: 177)
  • 7.25 泛型数学 <tgmath.h> (p: 272-273)
  • F.10.3.2 exp2 函数 (p: 379)
  • C11 标准 (ISO/IEC 9899:2011):
  • 7.12.6.2 exp2 函数 (p: 242-243)
  • 7.25 泛型数学 <tgmath.h> (p: 373-375)
  • F.10.3.2 exp2 函数 (p: 521)
  • C99标准(ISO/IEC 9899:1999):
  • 7.12.6.2 exp2函数(页码:223)
  • 7.22 泛型数学 <tgmath.h>(页码:335-337)
  • F.9.3.2 exp2函数(页码:458)

参阅

(C99) (C99)
计算 e 的给定次幂 ( e x )
(函数)
(C99) (C99) (C99)
计算 e 的给定次幂减一 ( e x -1 )
(函数)
(C99) (C99) (C99)
计算以 2 为底的对数 ( log 2 (x) )
(函数)
C++ 文档 关于 exp2