Namespaces
Variants

std:: exp, std:: expf, std:: expl

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
定义于头文件 <cmath>
(1)
float exp ( float num ) ;

double exp ( double num ) ;

long double exp ( long double num ) ;
(C++23 前)
/*floating-point-type*/
exp ( /*floating-point-type*/ num ) ;
(C++23 起)
(C++26 起为 constexpr)
float expf ( float num ) ;
(2) (C++11 起)
(C++26 起为 constexpr)
long double expl ( long double num ) ;
(3) (C++11 起)
(C++26 起为 constexpr)
SIMD 重载 (C++26 起)
定义于头文件 <simd>
template < /*math-floating-point*/ V >

constexpr /*deduced-simd-t*/ < V >

exp ( const V & v_num ) ;
(S) (C++26 起)
额外重载 (C++11 起)
定义于头文件 <cmath>
template < class Integer >
double exp ( Integer num ) ;
(A) (C++26 起为 constexpr)
1-3) 计算 e 欧拉数 2.7182818 ... )的给定幂次 num 标准库为所有 cv 未限定浮点类型提供了 std::exp 的重载版本作为参数类型。 (C++23 起)
S) SIMD重载对 v_num 执行逐元素的 std::exp 运算。
(定义参见 math-floating-point deduced-simd-t
(since C++26)
A) 为所有整数类型提供了额外的重载,这些类型被视为 double
(since C++11)

目录

参数

num - 浮点数或整数值

返回值

如果未发生错误,则返回 num 的以 e 为底的指数值( e num
)。

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

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

错误处理

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

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

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

注释

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

额外的重载并不要求完全按照 (A) 的形式提供。只需确保对于整数类型的实参 num std :: exp ( num ) 能够产生与 std :: exp ( static_cast < double > ( num ) ) 相同的效果即可。

示例

#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <numbers>
// #pragma STDC FENV_ACCESS ON
consteval double approx_e()
{
    long double e{1.0};
    for (auto fac{1ull}, n{1llu}; n != 18; ++n, fac *= n)
        e += 1.0 / fac;
    return e;
}
int main()
{
    std::cout << std::setprecision(16)
              << "exp(1) = e¹ = " << std::exp(1) << '\n'
              << "numbers::e  = " << std::numbers::e << '\n'
              << "approx_e    = " << approx_e() << '\n'
              << "FV of $100, continuously compounded at 3% for 1 year = "
              << std::setprecision(6) << 100 * std::exp(0.03) << '\n';
    // 特殊值
    std::cout << "exp(-0) = " << std::exp(-0.0) << '\n'
              << "exp(-Inf) = " << std::exp(-INFINITY) << '\n';
    // 错误处理
    errno = 0;
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "exp(710) = " << std::exp(710) << '\n';
    if (errno == ERANGE)
        std::cout << "    errno == ERANGE: " << std::strerror(errno) << '\n';
    if (std::fetestexcept(FE_OVERFLOW))
        std::cout << "    FE_OVERFLOW raised\n";
}

可能的输出:

exp(1) = e¹ = 2.718281828459045
numbers::e  = 2.718281828459045
approx_e    = 2.718281828459045
FV of $100, continuously compounded at 3% for 1 year = 103.045
exp(-0) = 1
exp(-Inf) = 0
exp(710) = inf
    errno == ERANGE: 数值结果超出范围
    FE_OVERFLOW raised

参阅

(C++11) (C++11) (C++11)
返回 2 的给定次幂 ( 2 x )
(函数)
(C++11) (C++11) (C++11)
返回 e 的给定次幂减 1 ( e x -1 )
(函数)
(C++11) (C++11)
计算自然(以 e 为底)对数 ( ln(x) )
(函数)
复数以 e 为底的指数函数
(函数模板)
对 valarray 的每个元素应用函数 std::exp
(函数模板)
C 文档 for exp