Exp
定义于头文件 <cmath>
中。
描述
计算 e
(欧拉数, 2.7182818...) 的 num
次幂。
该库为所有 cv-unqualified 浮点类型提供了 std::exp
的重载,作为参数 num
的类型。
声明
- C++23
- C++11
// 1)
/* floating-point-type */ exp( /* floating-point-type */ num );
// 2)
float expf( float num );
// 3)
long double expl( long double num );
// 4)
template< class Integer >
double exp ( Integer num );
// 1)
float exp ( float num );
// 2)
double exp ( double num );
// 3)
long double exp ( long double num );
// 4)
float expf( float num );
// 5)
long double expl( long double num );
// 6)
template< class Integer >
double exp ( Integer num );
参数
num
- 浮点或整数值
返回值
如果没有发生错误,返回 num
的以 e 为底的指数。
如果因溢出导致范围错误,则返回 +HUGE_VAL
、+HUGE_VALF
或 +HUGE_VALL
。
如果因下溢导致范围错误,则返回正确结果(舍入后)。
错误处理
错误按 math_errhandling 中指定的方式报告。
如果实现支持 IEEE 浮点运算(IEC 60559)
如果参数是 ±0
,返回 1
;如果参数是 -∞
,返回 +0
;如果参数是 +∞
,返回 +∞
;如果参数是 NaN,返回 NaN。
备注
对于 IEEE 兼容的 double 类型,如果 709.8 < num
,则保证溢出;如果 num < -708.4
,则保证下溢。
不要求提供的附加重载与附加重载完全一致。它们只需足以确保对于整数类型的参数 num,
std::exp(num)
具有与 std::exp(static_cast<double>(num))
相同的效果。
示例
#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <iostream>
#include <iomanip>
// #pragma STDC FENV_ACCESS ON
int main()
{
std::cout
<< "exp(1) = e¹ = "
<< std::setprecision(16) << std::exp(1) << '\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
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