C++ 参考手册

位置:首页 > C++ 参考手册 >数值库 >常用数学函数 > std::exp, std::expf, std::expl

定义于头文件 <cmath>
(1)
float       exp ( float arg );
float       expf( float arg );
(C++11 起)
double      exp ( double arg );
(2)
(3)
long double exp ( long double arg );
long double expl( long double arg );
(C++11 起)
double      exp ( IntegralType arg );
(4) (C++11 起)

计算 e (欧拉数, 2.7182818 )的 arg 次幂。

4) 接受任何整数类型参数的重载集或函数模板。等价于 2) (将参数转型为 double )。

参数

arg - 浮点或整数类型

返回值

若不出现错误,则返回 arg 的底 e 指数( earg
)。

若出现上溢所致的值域错误,则返回 +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 <iostream>
#include <cmath>
#include <cerrno>
#include <cstring>
#include <cfenv>
#pragma STDC FENV_ACCESS ON
int main()
{
    std::cout << "exp(1) = " << std::exp(1) << '\n'
              << "FV of $100, continuously compounded at 3% for 1 year = "
              << 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) = 2.71828
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 的给定次幂减一( ex-1
(函数)
(C++11)(C++11)
计算自然(以 e 为底)对数( ln(x)
(函数)
e 为底复数的指数
(函数模板)
应用函数 std::exp 到 valarray 的每个元素
(函数模板)