C
数值 | Numerics

exp2

exp2, exp2f, exp2l

在头文件中定义
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-floating point value

返回值

如果没有错误发生,则n(2n)的基2指数

)被返回。

如果发生由溢出引起的范围错误,则返回+ HUGE_VAL,+ HUGE_VALF或+ HUGE_VALL。

如果由于下溢而发生范围错误,则返回正确的结果(舍入后)。

错误处理

按照math_errhandling中的指定报告错误。

如果实现支持IEEE浮点运算(IEC 60559),

  • 如果参数是±0,则返回1

#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) // special values printf("exp2(-9) = %f\n", exp2(-0.9) printf("exp2(-Inf) = %f\n", exp2(-INFINITY) //error handling 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(-9) = 0.535887 exp2(-Inf) = 0.000000 exp2(1024) = Inf errno == ERANGE: Result too large FE_OVERFLOW raised

参考

  • C11标准(ISO / IEC 9899:2011):

扩展内容

Expexpfexpl(C99)(C99)计算e给定的倍率(ex)(函数)
expper1expm1fexpm1l(C99)(C99)(C99)计算e增加到给定的倍率,减去一个(ex-1)(函数)
log2log2flog21(C99)(C99)(C99)计算以2为底的对数(log2(x))(函数)

|exp2的 C ++文档 |