Namespaces
Variants

exp, expf, expl

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
exp
(C23)
(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 expf ( float arg ) ;
(1) (C99 起)
double exp ( double arg ) ;
(2)
long double expl ( long double arg ) ;
(3) (C99 起)
定义于头文件 <tgmath.h>
#define exp( arg )
(4) (C99 起)
1-3) 计算 e 欧拉数 2.7182818 ... )的给定幂次 arg
4) 类型泛型宏:若 arg 具有 long double 类型,则调用 expl 。否则,若 arg 具有整数类型或 double 类型,则调用 exp 。否则调用 expf 。若 arg 为复数或虚数,则该宏将调用对应的复数函数( cexpf cexp cexpl )。

目录

参数

arg - 浮点数值

返回值

如果没有错误发生,将返回 arg 的以 e 为底的指数函数值( e arg
)。

如果由于溢出发生范围错误,将返回 +HUGE_VAL +HUGE_VALF +HUGE_VALL

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

错误处理

错误报告方式遵循 math_errhandling 中的规范。

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

  • 若参数为 ±0,则返回 1
  • 若参数为 -∞,则返回 +0
  • 若参数为 +∞,则返回 +∞
  • 若参数为 NaN,则返回 NaN

注释

对于 IEEE 兼容类型 double ,当 709.8 < arg 时保证会发生溢出,当 arg < -708.4 时保证会发生下溢。

示例

#include <errno.h>
#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
int main(void)
{
    printf("exp(1) = %f\n", exp(1));
    printf("FV of $100, continuously compounded at 3%% for 1 year = %f\n",
            100*exp(0.03));
    // 特殊值
    printf("exp(-0) = %f\n", exp(-0.0));
    printf("exp(-Inf) = %f\n", exp(-INFINITY));
    // 错误处理
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("exp(710) = %f\n", exp(710));
    if (errno == ERANGE)
        perror("    errno == ERANGE");
    if (fetestexcept(FE_OVERFLOW))
        puts("    FE_OVERFLOW raised");
}

可能的输出:

exp(1) = 2.718282
FV of $100, continuously compounded at 3% for 1 year = 103.045453
exp(-0) = 1.000000
exp(-Inf) = 0.000000
exp(710) = inf
    errno == ERANGE: Numerical result out of range
    FE_OVERFLOW raised

参考文献

  • C23 标准 (ISO/IEC 9899:2024):
  • 7.12.6.1 exp 函数 (p: TBD)
  • 7.25 类型泛型数学 <tgmath.h> (p: TBD)
  • F.10.3.1 exp 函数 (p: TBD)
  • C17 标准 (ISO/IEC 9899:2018):
  • 7.12.6.1 exp 函数集 (p: 175)
  • 7.25 泛型数学 <tgmath.h> (p: 272-273)
  • F.10.3.1 exp 函数集 (p: 379)
  • C11 标准 (ISO/IEC 9899:2011):
  • 7.12.6.1 exp 函数 (p: 242)
  • 7.25 类型泛型数学 <tgmath.h> (p: 373-375)
  • F.10.3.1 exp 函数 (p: 520)
  • C99标准(ISO/IEC 9899:1999):
  • 7.12.6.1 exp函数(第223页)
  • 7.22 泛型数学 <tgmath.h>(第335-337页)
  • F.9.3.1 exp函数(第458页)
  • C89/C90 标准 (ISO/IEC 9899:1990):
  • 4.5.4.1 exp 函数

参考

(C99) (C99) (C99)
计算2的给定次幂 ( 2 x )
(函数)
(C99) (C99) (C99)
计算e的给定次幂减一 ( e x -1 )
(函数)
(C99) (C99)
计算自然(以e为底)对数 ( ln(x) )
(函数)
(C99) (C99) (C99)
计算复数的以e为底的指数
(函数)
C++ 文档 for exp