命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | 数值‎ | 数学
 
 
 
常用数学函数
函数
基本操作
(C++11)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
指数函数
exp
(C++11)
(C++11)

(C++11)
(C++11)
幂函数
(C++11)
(C++11)
三角
双曲函数
(C++11)
(C++11)
(C++11)

误差函数和伽马函数
(C++11)
(C++11)
(C++11)
(C++11)
取整浮点运算
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
浮点操纵函数
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
分类和比较
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
类型
(C++11)
(C++11)
(C++11)
宏常量
分类
(C++11)(C++11)(C++11)(C++11)(C++11)


 
定义于头文件 <cmath>
(1)
float       exp ( float num );

double      exp ( double num );

long double exp ( long double num );
(直至 C++23)
/*浮点类型*/
            exp ( /*浮点类型*/ 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< /*数学浮点类型*/ V >

constexpr /*推导 SIMD 类型*/<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-unqualified 浮点类型提供了 std::exp 的重载作为参数类型。(C++23 起)
S) SIMD 重载对 v_num 执行逐元素的 std::exp
(有关它们的定义,请参阅 math-floating-pointdeduced-simd-t。)
(C++26 起)
A) 为所有整数类型提供了额外的重载,它们被视为 double
(C++11 起)

目录

[编辑] 参数

num - 浮点值或整数值

[编辑] 返回值

如果没有发生错误,返回 num 的自然指数(enum
)。

如果由于溢出导致范围错误,则返回 +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) 提供额外的重载。它们只需要足以确保对于整数类型的参数 numstd::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';
 
    // special values
    std::cout << "exp(-0) = " << std::exp(-0.0) << '\n'
              << "exp(-Inf) = " << std::exp(-INFINITY) << '\n';
 
    // error handling 
    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: Numerical result out of range
    FE_OVERFLOW raised

[编辑] 另见

(C++11)(C++11)(C++11)
返回 2 的给定幂(2x
(函数) [编辑]
(C++11)(C++11)(C++11)
返回 e 的给定幂减去 1(ex-1
(函数) [编辑]
(C++11)(C++11)
计算自然(底数为 e)对数(ln(x)
(函数) [编辑]
复数 e 的指数
(函数模板) [编辑]
将函数 std::exp 应用于 valarray 的每个元素
(函数模板) [编辑]
C 文档 用于 exp